带有圆角脑卒中位图图像 [英] Bitmap image with rounded corners with stroke
问题描述
我有锐利边缘的图像。
I have a image with sharp edges.
在tile_mode.xml
the 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
the 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
layout.xml
<LinearLayout
android:id="@+id/frame1"
android:background="@drawable/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
我设置的图片作为背景,以这个布局,并绘制它的边界,但问题是图像是方形的有锐边和我在XML正在绘制边框圆角。因此,如何使图像还带有圆角?
I am setting the image as a background to this layout and drawing a border to it but the problem is the image is square with sharp edges and the border which I am drawing in the xml is rounded corners. So how to make the image also with rounded corners?
推荐答案
这是解决方案之一,你必须进行一轮主布局的背景
并保持内部你的的ImageView与你的愿望图像:
This is one of solution in which you have to make round to your main layout background
and inside keep your 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 *
*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>
更新
挖地段后,我碰到对的解决方案,它已经张贴在计算器
After digging lots, I came across on of solution which already posted on stackoverflow
<一个href="http://stackoverflow.com/questions/2459916/how-to-make-an-imageview-to-have-rounded-corners">How作出的ImageView有圆角
第一步@
的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>
第二步:@
Step2@
请一个函数使用的帆布这使得整到你的位图。
Make one function which make rounded to your bitmap using 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;
}
第三步:@
step3@
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));
这篇关于带有圆角脑卒中位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!