android 实现圆角图片解决方案
程序员文章站
2023-12-05 16:49:58
现在我们就来看看怎么样把图片的四角都变成圆形的,为什么要这样做那,如果要是这样界面就会非常的美观,下面我们就来看看代码吧。 java代码: 复制代码 代码如下: publi...
现在我们就来看看怎么样把图片的四角都变成圆形的,为什么要这样做那,如果要是这样界面就会非常的美观,下面我们就来看看代码吧。
java代码:
public static bitmap toroundcorner(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;
}
当我们需要圆角的时候,调用这个方法,第一个参数是传入需要转化成圆角的图片,第二个参数是圆角的度数,数值越大,圆角越大
下面兴趣个例子,我有一个linearlayout,我想把它的背景图片设置成圆角
布局文件main.xml
java代码:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</linearlayout>
java代码:
import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.porterduff.mode;
import android.graphics.porterduffxfermode;
import android.graphics.rect;
import android.graphics.rectf;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
import android.os.bundle;
import android.widget.imageview;
import android.widget.linearlayout;
public class myactivity extends activity {
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
// drawable drawable = getresources().getdrawable(r.drawable.bg);
// bitmapdrawable bitmapdrawable = (bitmapdrawable) drawable;
// bitmap bitmap = bitmapdrawable.getbitmap();
linearlayout layout = (linearlayout) findviewbyid(r.id.layout);
drawable drawable = getresources().getdrawable(r.drawable.bg);
bitmapdrawable bitmapdrawable = (bitmapdrawable) drawable;
bitmap bitmap = bitmapdrawable.getbitmap();
bitmapdrawable bbb = new bitmapdrawable(toroundcorner(bitmap, 30));
layout.setbackgrounddrawable(bbb);
//imageview imageview = (imageview) findviewbyid(r.id.imgshow);
//imageview.setimagebitmap(myactivity.getroundedcornerbitmap(bitmap));
//imageview.setimagebitmap(myactivity.toroundcorner(bitmap, 20));
}
public static bitmap toroundcorner(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;
}
}
java代码:
复制代码 代码如下:
public static bitmap toroundcorner(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;
}
当我们需要圆角的时候,调用这个方法,第一个参数是传入需要转化成圆角的图片,第二个参数是圆角的度数,数值越大,圆角越大
下面兴趣个例子,我有一个linearlayout,我想把它的背景图片设置成圆角
布局文件main.xml
java代码:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</linearlayout>
java代码:
复制代码 代码如下:
import android.app.activity;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.porterduff.mode;
import android.graphics.porterduffxfermode;
import android.graphics.rect;
import android.graphics.rectf;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
import android.os.bundle;
import android.widget.imageview;
import android.widget.linearlayout;
public class myactivity extends activity {
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
// drawable drawable = getresources().getdrawable(r.drawable.bg);
// bitmapdrawable bitmapdrawable = (bitmapdrawable) drawable;
// bitmap bitmap = bitmapdrawable.getbitmap();
linearlayout layout = (linearlayout) findviewbyid(r.id.layout);
drawable drawable = getresources().getdrawable(r.drawable.bg);
bitmapdrawable bitmapdrawable = (bitmapdrawable) drawable;
bitmap bitmap = bitmapdrawable.getbitmap();
bitmapdrawable bbb = new bitmapdrawable(toroundcorner(bitmap, 30));
layout.setbackgrounddrawable(bbb);
//imageview imageview = (imageview) findviewbyid(r.id.imgshow);
//imageview.setimagebitmap(myactivity.getroundedcornerbitmap(bitmap));
//imageview.setimagebitmap(myactivity.toroundcorner(bitmap, 20));
}
public static bitmap toroundcorner(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;
}
}