圆angular与中风的位图图像

我有一个锐利的形象。 在这里输入图像说明

tile_mode.xml

<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/background" android:tileMode="repeat"> </bitmap> 

back.xml

 <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tile_mode" /> <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> 

layout.xml

 <LinearLayout android:id="@+id/frame1" android:background="@drawable/back" android:layout_width="fill_parent" android:layout_height="wrap_content"> </LinearLayout> 

我设置图像作为这种布局的背景,并绘制一个边框,但问题是图像是正方形的尖锐的边缘,我在xml中绘制的边框是圆angular。 那么如何使图像也是圆angular呢?

这是解决scheme之一,你必须make round to your main layout background并保持你的imageview with your desire image:

如下所示:

back.xml

这将使你的形象转angular

 <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#dd7b7a"/> <corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp"/> <solid android:color="#dd7b7a"/> </shape> 

tile_mode.xml

 <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/background" android:tileMode="repeat" /> 

layout.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <LinearLayout android:padding="4dip" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/back" android:gravity="center_horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tile_mode" /> </LinearLayout> </LinearLayout> 

更新

挖了很多之后,我碰到了已经发布在stackoverflow上的解决scheme

将图像更改为圆angular

如何使ImageView具有圆angular

步骤1@

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> 

第2步@

做一个函数,使用canvas四舍五入你的位图。

 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap .getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } 

步骤3 @

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView image=(ImageView)findViewById(R.id.image); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.testing); image.setImageBitmap(getRoundedCornerBitmap(bitmap, 20)); 

RobinHood的答案对我有一个变化,因为我收到有关variables赋值的错误。

我不得不改变线路:

  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 

到这一行:

  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 

使我的完整代码如下:

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); TextView textViewTitle = (TextView) findViewById(R.id.MYTEXTIDHERE); textViewTitle.setText("Some Text"); ImageButton imageButtonSetter = (ImageButton) findViewById(R.id.MYIMAGEIDHERE); Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.MYIMAGENAMEHERE); imageButtonSetter.setImageBitmap(getRoundedCornerBitmap(myBitmap, 40)); } public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap .getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } 

实际上,我有一个库,可以完全满足你的需求,而且你不必去打扰那些XML文件。

https://github.com/pungrue26/SelectableRoundedImageView

通过这个开源项目, 您可以在每个angular落设置不同的半径,并设置边框(宽度和颜色)等,如下图所示。 我希望它可以帮助你。

CardView内部的圆角ImageView

试试你的back.xml这样的东西。

 <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ffffffff"/> <stroke android:width="3dp" android:color="#ff000000" /> <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" /> <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp"/> </shape> 

今天我有类似的问题,只有我的图像是一个谷歌地图。 你实际上不得不隐藏angular落,做的方法是创build一个9Patch,如下所示:

在这里输入图像说明

并将其作为背景应用于所有布局或覆盖布局的其他布局。 看看下面的链接了解更多信息:

有没有办法实现一个Mapfragment的圆angular?

我已经去过这个网站: http : //www.sumopaint.com/app/
并用手画出来,你可以拿我的例子,改变颜色为你喜欢的。 你可能需要下一个:

在这里输入图像说明

您可以在以下链接中获得有关如何创build9Patch的更多信息:

http://radleymarx.com/blog/simple-guide-to-9-patch/

http://android9patch.blogspot.co.il/

好吧,让我们尝试下面的方式

  <?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="30dp"/> <stroke android:width="2dp" android:color="#000000"/> </shape> <LinearLayout android:id="@+id/frame1" android:background="@drawable/corner_background" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/background" /> </LinearLayout> 
 <?xml version="1.0" encoding="utf-8"?> 

 <item> <bitmap android:src="@drawable/bg_striped_img" android:tileMode="repeat" /> </item> <item android:left="-20dp" android:top="-20dp" android:right="-20dp" android:bottom="-20dp"> <shape android:shape="oval" > <stroke android:width="20dp" android:color="#ffffffff" /> <solid android:color="#00000000" /> <size android:height="120dp" android:width="120dp" /> </shape> </item> <item > <shape android:shape="oval" > <stroke android:width="1dp" android:color="#ff999999" /> <solid android:color="#00000000" /> <size android:height="120dp" android:width="120dp" /> </shape> </item> 

将原始位图传递给下面的函数,您将得到一个四舍五入的位图作为结果:)。 希望这有助于某人。

  public Bitmap getRoundedBitmap(Bitmap bitmap) { Bitmap resultBitmap; int originalWidth = bitmap.getWidth(); int originalHeight = bitmap.getHeight(); float r; if (originalWidth > originalHeight) { resultBitmap = Bitmap.createBitmap(originalHeight, originalHeight, Bitmap.Config.ARGB_8888); r = originalHeight / 2; } else { resultBitmap = Bitmap.createBitmap(originalWidth, originalWidth, Bitmap.Config.ARGB_8888); r = originalWidth / 2; } Canvas canvas = new Canvas(resultBitmap); final Paint paint = new Paint(); final Rect rect = new Rect(ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE, originalWidth, originalHeight); paint.setAntiAlias(true); canvas.drawARGB(ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE); canvas.drawCircle(r, r, r, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return resultBitmap; }