我的图像边缘锐利。
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中绘制的边框是圆角。那么如何使图像也圆角?
答案 0 :(得分:16)
这是您必须make round to your main layout background
和内部保留imageview with your desire image:
如下所示:
back.xml
这会使您的图像成为圆角
<?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
Changing Image as Rounded Corner
How to make an ImageView to have rounded corners
步骤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 @
使用画布创建一个使您的位图四舍五入的函数。
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));
答案 1 :(得分:2)
由于我收到的关于变量赋值的错误,RobinHood的回答对我有所改变。
我不得不换行:
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;
}
答案 2 :(得分:2)
实际上,我创建了一个完全符合您需求的库,您不必费心去处理这些xml文件。
https://github.com/pungrue26/SelectableRoundedImageView
通过这个开源项目,您可以在每个角上设置不同的半径,并设置边框(宽度和颜色)等,如下所示。我希望它可以帮助你。
答案 3 :(得分:1)
好吧让我们尝试以下方式
<?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>
答案 4 :(得分:1)
将原始位图传递给以下函数,您将得到一个圆形位图作为结果:)。希望这有助于某人。
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;
}
答案 5 :(得分:0)
尝试你的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>
答案 6 :(得分:0)
今天我遇到了类似的问题,只有我的图片是Google地图。你实际上必须隐藏角落,这样做的方法是创建一个9Patch,如下所示:
并将其作为背景应用于所有布局或覆盖布局的其他布局。 请查看以下链接以获取更多信息:
Is there a way to implement rounded corners to a Mapfragment?
我已经去了这个网站:http://www.sumopaint.com/app/
并手工绘制,你可以拿我的榜样,根据自己的喜好改变颜色。
你可能需要下一个:
您可以通过以下链接获取有关如何创建9Patch的更多信息:
答案 7 :(得分:0)
<?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>