如何在 Android 中的 ImageView 末尾添加 Text?
How to add Text in the end of ImageView in Android?
我有一个 ListFragment
,我在 ListView
中使用 picasso
下载图像。这是我的 ArrayAdapter
代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_dish_view, parent, false);
}
DrawLineTransformationation myTransformation = new DrawLineTransformationation() {
@Override
public String key() {
return super.key();
}
};
Picasso
.with(context)
.load("http://i.imgur.com/rFLNqWI.jpg")
.transform(myTransformation)
.fit()
.into((ImageView) convertView);
return convertView;
}
private class DrawLineTransformationation implements Transformation {
@Override
public String key() {
// TODO Auto-generated method stub
return "drawline";
}
@Override
public Bitmap transform(Bitmap bitmap) {
// TODO Auto-generated method stub
synchronized (DrawLineTransformationation.class) {
if(bitmap == null) {
return null;
}
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(50);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("0", 10, 500, paint);
canvas.drawBitmap(bitmapImage, 900, 20, null);
bitmap.recycle();
return resultBitmap;
}
}
}
list_dish_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Roomimg"
android:layout_width="match_parent"
android:layout_height="200dp"/>
如果我运行上面的代码我可以看到这个view。
现在,我想在每个图像的末尾添加文本,并在右下角添加一个图像(这样一半是文字描述,另一半是大图像)。请参阅 this 示例。您可以在视图中看到文本和个人资料图片。我怎样才能实现我的情况?
尝试以下布局来实现您的设计。我在xml中使用了负边距,如果你想避免它,边距不能在计算后在activity中设置。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#FF000000"
android:src="@drawable/matrix" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:background="#FFFF00FF"
android:padding="10dp"
android:text="Some text........." />
<ImageView
android:id="@+id/profilePic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="@id/imageView"
android:layout_marginTop="-25dp"
android:src="@drawable/circle" />
</RelativeLayout>
我有一个 ListFragment
,我在 ListView
中使用 picasso
下载图像。这是我的 ArrayAdapter
代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_dish_view, parent, false);
}
DrawLineTransformationation myTransformation = new DrawLineTransformationation() {
@Override
public String key() {
return super.key();
}
};
Picasso
.with(context)
.load("http://i.imgur.com/rFLNqWI.jpg")
.transform(myTransformation)
.fit()
.into((ImageView) convertView);
return convertView;
}
private class DrawLineTransformationation implements Transformation {
@Override
public String key() {
// TODO Auto-generated method stub
return "drawline";
}
@Override
public Bitmap transform(Bitmap bitmap) {
// TODO Auto-generated method stub
synchronized (DrawLineTransformationation.class) {
if(bitmap == null) {
return null;
}
Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(50);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
canvas.drawText("0", 10, 500, paint);
canvas.drawBitmap(bitmapImage, 900, 20, null);
bitmap.recycle();
return resultBitmap;
}
}
}
list_dish_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Roomimg"
android:layout_width="match_parent"
android:layout_height="200dp"/>
如果我运行上面的代码我可以看到这个view。 现在,我想在每个图像的末尾添加文本,并在右下角添加一个图像(这样一半是文字描述,另一半是大图像)。请参阅 this 示例。您可以在视图中看到文本和个人资料图片。我怎样才能实现我的情况?
尝试以下布局来实现您的设计。我在xml中使用了负边距,如果你想避免它,边距不能在计算后在activity中设置。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#FF000000"
android:src="@drawable/matrix" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:background="#FFFF00FF"
android:padding="10dp"
android:text="Some text........." />
<ImageView
android:id="@+id/profilePic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="@id/imageView"
android:layout_marginTop="-25dp"
android:src="@drawable/circle" />
</RelativeLayout>