问题描述
我需要一个圆形的图像按钮,将图像剪成一个圆圈,但这就是我得到的->(见巴士图像)->bus image
我已经给出了Imagebutton xml和shape xml的描述。图像没有被剪成圆形,但看起来圆形被分成了 2 部分,中间添加了图像。我的代码如下->
代码:
layout/activity.xml:
<ImageButton
android:id="@+id/imageButtonGenerateBulk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Generate Qr Code"
android:textStyle="bold"
android:layout_alignEnd="@+id/tv_question"
android:layout_alignRight="@+id/tv_question"
android:background="@drawable/drawii2"
android:src="@drawable/bus"
android:layout_weight="1"/>
drawii2.xml:
<?xml version ="1.0" encoding ="utf-8"?><!-- Learn More about how to use App Actions: https://developer.android.com/guide/actions/index.html -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="40dp" />
<gradient
android:angle="45"
android:endColor="#01f1fa"
android:gradienTradius="41dp"
android:startColor="#0189ff"
android:type="linear" />
<!--android:centerX="float"
android:centerY="float"-->
<!--If your shape requires only one solid color-->
<!--<solid
android:color="#FFFFFF" />-->
<size
android:width="30dp"
android:height="30dp" />
<!--Use android:dashWidth="2dp" and android:dashGap="2dp"
to add dashes to your stroke-->
<stroke
android:width="2dp"
android:color="#FFFFFF" />
<!--If you want to add padding-->
<!-- <padding
android:left="10dp"
android:top="20dp"
android:right="40dp"
android:bottom="8dp" />-->
</shape>
也用过这个:
<?xml version ="1.0" encoding ="utf-8"?><!-- Learn More about how to use App Actions: https://developer.android.com/guide/actions/index.html -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#0000" />
<corners android:radius="20dp" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
解决方法
您可以使用 PorterDuff:
Drawable bt = //Get your picture drawable
Bitmap results = ((BitmapDrawable)bt).getBitmap();
Bitmap results32bit = Bitmap.createBitmap(results.getWidth(),results.getHeight(),Bitmap.Config.ARGB_8888);
Canvas tmpC = new Canvas(results32bit);
tmpC.drawBitmap(results,null);
Bitmap mutableBitmap = results32bit.copy(Bitmap.Config.ARGB_8888,true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
PorterDuff.Mode mode = PorterDuff.Mode.DST_ATOP;// choose a mode
paint.setXfermode(new PorterDuffXfermode(mode));
Bitmap bmAppBg = (((BitmapDrawable)(resizeImage(getResources().getDrawable( R.mipmap.round_corner_square_2),results.getWidth(),results.getHeight()))).getBitmap());
paint.setAlpha(255);
canvas.drawBitmap(bmAppBg,paint);
将您的 R.mipmap.round_corner_square_2 作为一个 png,它基本上是一个圆形,内部透明,外部填充
更多信息可以在以下位置找到: https://developer.android.com/reference/android/graphics/PorterDuff.Mode
,这段代码给了我一个圆形按钮:
<item android:state_enabled="false">
<layer-list>
<item android:id="@android:id/background">
<shape android:shape="oval">
<solid android:color="@color/colorHint" />
</shape>
</item>
</layer-list>
</item>
<item android:state_pressed="true">
<layer-list>
<item android:id="@android:id/background">
<shape android:shape="oval">
<solid android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list>
<item android:id="@android:id/background">
<shape android:shape="oval">
<solid android:color="@color/colorPrimaryLight" />
</shape>
</item>
</layer-list>
</item>
我认为您也可以将其用于图像查看。此 xml 文件用于处理按钮状态(按下、启用...),但您可以删除所有不需要的内容。将其设置为 ImageView 的背景后,您可以尝试使用 ImageView 的 android:scaleType 以适合您想要的照片