我将背景图像设置为LinearLayout,如下所示:
1.back_xml:
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/back" > </item> <item> <shape> <solid/> <stroke android:width="1dip" android:color="#225786" /> <corners android:radius="10dip"/> <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> </shape> </item>
2. tile.xml
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/pie_chart_background" android:tileMode="repeat"> </bitmap>
现在我将back.xml设置为LinearLayout的背景它工作正常.
我需要有一个带圆角的图像以及它的边框.但我只有圆角的边框而不是图像我的代码中的问题是什么我错过了什么?
这是我的图像的样子:
解决方法
花了几个小时的时间来讨论你的问题,最后我实现了,希望现在它会给你你想要的相同结果,请通过下面的代码告诉我它是否有效?
将适当的参数传递给下面的函数,以获得您想要的颜色边框的圆角.
public static Bitmap getRoundedCornerImage(Bitmap bitmap,int cornerDips,int borderDips,Context context) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) borderDips,context.getResources().getDisplayMetrics()); final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) cornerDips,context.getResources().getDisplayMetrics()); final Paint paint = new Paint(); final Rect rect = new Rect(0,bitmap.getWidth(),bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); paint.setColor(0xFFFFFFFF); paint.setStyle(Paint.Style.FILL); canvas.drawARGB(0,0); canvas.drawRoundRect(rectF,cornerSizePx,paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap,rect,paint); paint.setColor((Color.RED)); // you can change color of your border here,to other color paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth((float) borderSizePx); canvas.drawRoundRect(rectF,paint); return output; }
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> </RelativeLayout>
在OnCreate
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView rl=(ImageView)findViewById(R.id.image); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.testing); // your desire drawable image. rl.setImageBitmap(getRoundedCornerImage(bitmap,10,this)); }
原始图像
产量
以下链接帮助我实现我的目标:
Border over a bitmap with rounded corners in Android
Creating ImageView with round corners