6 个解决方案
#1
你可以让美工给图片啊,也可以自己画。
#2
自己画的话文字后面半透明背景怎么加呢?
#3
实在不行,就让美工给图。方便快捷
#4
在自定义view中 用一个长方形的textView放在圆形imageView下半层 然后调整叠放次序 将TextView放在imageView下面 简单说叠放层次就是 最下层(原imageView)->中间(TextView)->上层(透明圆形图形) 试试吧
#5
自定义view
package com.jisai.yuan;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class MyImageView_circle extends ImageView {
private Paint paint;
private int mWidth;
int mRadius;
private int mHeight;
String text;
int alpha,Fstatr,Fstop,textsize;
int bgcolor;
int textcolor;
private static final String TAG = "MyImageView";
public MyImageView_circle(Context context) {
super(context);
init("MyImageView(Context context)");
}
public MyImageView_circle(Context context, AttributeSet attrs) {
super(context, attrs);
init("MyImageView(Context context, AttributeSet attrs)");
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.myyuan);
text = mTypedArray.getString(R.styleable.myyuan_text);
alpha = mTypedArray.getInt(R.styleable.myyuan_alpha,100);
Fstatr= mTypedArray.getInt(R.styleable.myyuan_Fstatr,40);
Fstop= mTypedArray.getInt(R.styleable.myyuan_Fstop,100);
textsize= mTypedArray.getInt(R.styleable.myyuan_textsize,60);
bgcolor= mTypedArray.getColor(R.styleable.myyuan_bgcolor,Color.BLACK);
textcolor=mTypedArray.getColor(R.styleable.myyuan_bgcolor,Color.WHITE);
mTypedArray.recycle();
}
public MyImageView_circle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init("MyImageView(Context context, AttributeSet attrs, int defStyle)");
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.myyuan);
text = mTypedArray.getString(R.styleable.myyuan_text);
alpha = mTypedArray.getInt(R.styleable.myyuan_alpha,100);
Fstatr= mTypedArray.getInt(R.styleable.myyuan_Fstatr,40);
Fstop= mTypedArray.getInt(R.styleable.myyuan_Fstop,100);
textsize= mTypedArray.getInt(R.styleable.myyuan_textsize,70);
bgcolor= mTypedArray.getColor(R.styleable.myyuan_bgcolor,Color.BLACK);
textcolor=mTypedArray.getColor(R.styleable.myyuan_textcolor,Color.WHITE);
mTypedArray.recycle();
}
private void init(String structName) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int specSize = MeasureSpec.getSize(widthMeasureSpec);
mWidth = specSize;
specSize = MeasureSpec.getSize(heightMeasureSpec);
mHeight = specSize;
setMeasuredDimension(mWidth, mHeight);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
if (!(drawable instanceof BitmapDrawable)) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
if (null == b) {
return;
}
Bitmap bitmap = b.copy(Config.ARGB_8888, true);
int min = Math.min(mWidth, mHeight);
Bitmap roundBitmap = getCroppedBitmap(bitmap, min);
canvas.drawBitmap(roundBitmap, 0, 0, null);
paint=new Paint();
paint.setColor(bgcolor);
paint.setAlpha(alpha);
paint.setStyle(Paint.Style.FILL);
RectF re1= new RectF(0,0,mWidth,mWidth);
canvas.drawArc(re1,Fstatr,Fstop,false,paint);
paint.setAlpha(255);
paint.setTextSize(textsize);
paint.setColor(textcolor);
canvas.drawText(text,(mWidth-text.length()*textsize)/2,mWidth-textsize/2,paint);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
private Bitmap createRoundConerImage(Bitmap source) {
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rect, mRadius, mRadius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
}
XML中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android_custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jisai.yuan.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon"
android:layout_width="500dp"
android:layout_height="500dp"
android_custom:text="张三"
android:src="@mipmap/feb"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon2"
android:layout_width="300dp"
android:layout_height="300dp"
android_custom:text="王麻子"
android_custom:textsize="45"
android_custom:textcolor="@color/G"
android_custom:bgcolor="@color/RED"
android_custom:alpha="100"
android_custom:Fstatr="40"
android_custom:Fstop="100"
android:src="@mipmap/f2"/>
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon3"
android:layout_width="200dp"
android:layout_height="200dp"
android_custom:text="刘德华"
android_custom:textsize="35"
android_custom:textcolor="@color/G"
android_custom:bgcolor="@color/BLUE"
android_custom:alpha="100"
android_custom:Fstatr="40"
android_custom:Fstop="100"
android:src="@mipmap/f1"/>
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon4"
android:layout_width="100dp"
android:layout_height="100dp"
android_custom:text="周杰伦"
android_custom:textsize="20"
android_custom:textcolor="@color/RED"
android_custom:bgcolor="@color/colorPrimary"
android_custom:alpha="100"
android_custom:Fstatr="40"
android_custom:Fstop="100"
android:src="@mipmap/f3"/>
</LinearLayout>
</LinearLayout>
attrs
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="myyuan">
<attr name="text" format="string"/>
<attr name="textsize" format="integer"/>
<attr name="textcolor" format="color"/>
<attr name="bgcolor" format="color"/>
<attr name="alpha" format="integer"/>
<attr name="Fstatr" format="integer"/>
<attr name="Fstop" format="integer"/>
</declare-styleable>
</resources>
#1
你可以让美工给图片啊,也可以自己画。
#2
自己画的话文字后面半透明背景怎么加呢?
#3
实在不行,就让美工给图。方便快捷
#4
在自定义view中 用一个长方形的textView放在圆形imageView下半层 然后调整叠放次序 将TextView放在imageView下面 简单说叠放层次就是 最下层(原imageView)->中间(TextView)->上层(透明圆形图形) 试试吧
#5
自定义view
package com.jisai.yuan;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class MyImageView_circle extends ImageView {
private Paint paint;
private int mWidth;
int mRadius;
private int mHeight;
String text;
int alpha,Fstatr,Fstop,textsize;
int bgcolor;
int textcolor;
private static final String TAG = "MyImageView";
public MyImageView_circle(Context context) {
super(context);
init("MyImageView(Context context)");
}
public MyImageView_circle(Context context, AttributeSet attrs) {
super(context, attrs);
init("MyImageView(Context context, AttributeSet attrs)");
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.myyuan);
text = mTypedArray.getString(R.styleable.myyuan_text);
alpha = mTypedArray.getInt(R.styleable.myyuan_alpha,100);
Fstatr= mTypedArray.getInt(R.styleable.myyuan_Fstatr,40);
Fstop= mTypedArray.getInt(R.styleable.myyuan_Fstop,100);
textsize= mTypedArray.getInt(R.styleable.myyuan_textsize,60);
bgcolor= mTypedArray.getColor(R.styleable.myyuan_bgcolor,Color.BLACK);
textcolor=mTypedArray.getColor(R.styleable.myyuan_bgcolor,Color.WHITE);
mTypedArray.recycle();
}
public MyImageView_circle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init("MyImageView(Context context, AttributeSet attrs, int defStyle)");
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.myyuan);
text = mTypedArray.getString(R.styleable.myyuan_text);
alpha = mTypedArray.getInt(R.styleable.myyuan_alpha,100);
Fstatr= mTypedArray.getInt(R.styleable.myyuan_Fstatr,40);
Fstop= mTypedArray.getInt(R.styleable.myyuan_Fstop,100);
textsize= mTypedArray.getInt(R.styleable.myyuan_textsize,70);
bgcolor= mTypedArray.getColor(R.styleable.myyuan_bgcolor,Color.BLACK);
textcolor=mTypedArray.getColor(R.styleable.myyuan_textcolor,Color.WHITE);
mTypedArray.recycle();
}
private void init(String structName) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int specSize = MeasureSpec.getSize(widthMeasureSpec);
mWidth = specSize;
specSize = MeasureSpec.getSize(heightMeasureSpec);
mHeight = specSize;
setMeasuredDimension(mWidth, mHeight);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
if (!(drawable instanceof BitmapDrawable)) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
if (null == b) {
return;
}
Bitmap bitmap = b.copy(Config.ARGB_8888, true);
int min = Math.min(mWidth, mHeight);
Bitmap roundBitmap = getCroppedBitmap(bitmap, min);
canvas.drawBitmap(roundBitmap, 0, 0, null);
paint=new Paint();
paint.setColor(bgcolor);
paint.setAlpha(alpha);
paint.setStyle(Paint.Style.FILL);
RectF re1= new RectF(0,0,mWidth,mWidth);
canvas.drawArc(re1,Fstatr,Fstop,false,paint);
paint.setAlpha(255);
paint.setTextSize(textsize);
paint.setColor(textcolor);
canvas.drawText(text,(mWidth-text.length()*textsize)/2,mWidth-textsize/2,paint);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
private Bitmap createRoundConerImage(Bitmap source) {
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rect, mRadius, mRadius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
}
XML中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android_custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jisai.yuan.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon"
android:layout_width="500dp"
android:layout_height="500dp"
android_custom:text="张三"
android:src="@mipmap/feb"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon2"
android:layout_width="300dp"
android:layout_height="300dp"
android_custom:text="王麻子"
android_custom:textsize="45"
android_custom:textcolor="@color/G"
android_custom:bgcolor="@color/RED"
android_custom:alpha="100"
android_custom:Fstatr="40"
android_custom:Fstop="100"
android:src="@mipmap/f2"/>
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon3"
android:layout_width="200dp"
android:layout_height="200dp"
android_custom:text="刘德华"
android_custom:textsize="35"
android_custom:textcolor="@color/G"
android_custom:bgcolor="@color/BLUE"
android_custom:alpha="100"
android_custom:Fstatr="40"
android_custom:Fstop="100"
android:src="@mipmap/f1"/>
<com.jisai.yuan.MyImageView_circle
android:id="@+id/main_icon4"
android:layout_width="100dp"
android:layout_height="100dp"
android_custom:text="周杰伦"
android_custom:textsize="20"
android_custom:textcolor="@color/RED"
android_custom:bgcolor="@color/colorPrimary"
android_custom:alpha="100"
android_custom:Fstatr="40"
android_custom:Fstop="100"
android:src="@mipmap/f3"/>
</LinearLayout>
</LinearLayout>
attrs
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="myyuan">
<attr name="text" format="string"/>
<attr name="textsize" format="integer"/>
<attr name="textcolor" format="color"/>
<attr name="bgcolor" format="color"/>
<attr name="alpha" format="integer"/>
<attr name="Fstatr" format="integer"/>
<attr name="Fstop" format="integer"/>
</declare-styleable>
</resources>