欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 设置圆角图片实现代码

程序员文章站 2023-12-05 17:46:10
复制代码 代码如下:
复制代码 代码如下:

<?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>
package com.test.demo;

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;
}
}