【android中级】之android 背景圆角和图片圆角-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

【android中级】之android 背景圆角和图片圆角

简介: android背景圆角的实现 1.建立一个xml文件,命名为circle_corner,放置到drawable目录下,内容如下:                       圆角效果二 2.
android背景圆角的实现
1.建立一个xml文件,命名为circle_corner,放置到drawable目录下,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android">
  <gradient android:endColor="@android:color/transparent" 

                  android:startColor="#000000"

                  android:gradientRadius="60"  

                   android:type="radial"/>
        <corners android:radius="10px"/>
</shape>

            圆角效果二

<?xml version="1.0" encoding="UTF-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#99FFFFFF"/>

<corners android:radius="30px"/>

<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />

</shape>

2.在需要圆角背景的元件上设置背景为circle_corner

<TextView android:text="abc"

                android:background="@drawable/circle_corner"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"
>

  1. 图片本身加上圆角

Bitmap myCoolBitmap = ... ; // <-- Your bitmap you want rounded

int w = myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();


Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(rounder);  


Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

xferPaint.setColor(Color.RED);


canvas.drawRoundRect(new RectF(0,0,w,h), 20.0f, 20.0f, xferPaint);


xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

canvas.drawBitmap(myCoolBitmap, 0,0, null);

canvas.drawBitmap(rounder, 0, 0, xferPaint);

或者

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

  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 = 12;

    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;

}


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: