android 将图片变圆角,Android下图片变圆角的代码封装

main.xml

android:id="@+id/layout"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/listview"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_marginLeft="50dp"

android:cacheColorHint="#000"

android:divider="#000"

android:dividerHeight="5px"

android:fadingEdge="none"

android:scrollbars="none"

android:scrollingCache="true" />

package com.lian.image;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.PixelFormat;

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.util.AttributeSet;

import android.widget.ImageView;

public class SetImage extends ImageView {

public SetImage(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public SetImage(Context context, AttributeSet attrs) {

super(context, attrs, 0);

}

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

Path clipPath = new Path();

int w = this.getWidth();

int h = this.getHeight();

clipPath.addRoundRect(new RectF(0, 0, w, h), 10.0f, 10.0f, Path.Direction.CW);

canvas.clipPath(clipPath);

super.onDraw(canvas);

}

public void setImageDrawable(Drawable drawable, int pixels) {

// TODO Auto-generated method stub

Bitmap bitmap =toRoundCorner(drawableToBitmap(drawable), pixels);

Drawable drawable1 = new BitmapDrawable(bitmap);

super.setImageDrawable(drawable1);

}

public void setImageBitmap(Drawable drawable, int pixels) {

// TODO Auto-generated method stub

super.setImageBitmap(toRoundCorner(drawableToBitmap(drawable), pixels));

}

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

//创建一个和原始图片一样大小位图

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),

bitmap.getHeight(), Config.ARGB_8888);

//创建带有位图bitmap的画布

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;

}

public static Bitmap drawableToBitmap(Drawable drawable){

int width = drawable.getIntrinsicWidth();

int height = drawable.getIntrinsicHeight();

//创建一个和原始图片一样大小位图

Bitmap bitmap = Bitmap.createBitmap(width, height,

drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

: Bitmap.Config.RGB_565);//创建一个指定高、宽的可变的Bitmap图像

//创建带有位图bitmap的画布

Canvas canvas = new Canvas(bitmap);

drawable.setBounds(0,0,width,height);

drawable.draw(canvas);

return bitmap;

}

} package com.lian.image;

import java.util.ArrayList;

import java.util.HashMap;

import android.app.Activity;

import android.content.Context;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ListView;

public class SetDrawable extends Activity {

private ArrayList> data;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/* Drawable drawable1 = getResources().getDrawable(R.drawable.benchi1);

SetImage imageView1 = (SetImage) findViewById(R.id.show);

imageView1.setImageDrawable(drawable1, 20);

imageView1.setImageBitmap(drawable1, 20);*/

data = getData();

ListView list = (ListView)findViewById(R.id.listview);

MyAdapter adapter = new MyAdapter(SetDrawable.this, data, R.layout.list);

list.setAdapter(adapter);

}

private ArrayList> getData(){

ArrayList> list = new ArrayList>();

HashMap map = new HashMap();

for(int i = 0 ; i<100 ; i++){

map.put("show",R.drawable.benchi1);

list.add(map);

}

return list;

}

class ViewHolder{

public SetImage image;

}

class MyAdapter extends BaseAdapter{

//定义一个LayoutInflater来导入资料文件

LayoutInflater inflater;

//用来接管要绑定的数据

ArrayList> arrayList;

//要绑定的资料文件id

int resID;

public MyAdapter(Context context,

ArrayList> arrayList, int resID) {

this.inflater = LayoutInflater.from(context);

this.arrayList = arrayList;

this.resID = resID;

}

//绑定的数据的长度,也就是ListView项的个数

@Override

public int getCount() {

// TODO Auto-generated method stub

return this.arrayList.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

//创建一个ViewHolder来盛放控件

ViewHolder vHolder;

//经由过程断定convertView是否为空来取得ViewHolder对象

//这个convertView就是ListView的一行

if (convertView == null) {

//若是为null则从布局文件中导入

convertView=inflater.inflate(resID, null);

vHolder=new ViewHolder();

vHolder.image = (SetImage)convertView.findViewById(R.id.show);

//setTag办法用来设置与视图接洽关系的标签,我的懂得就是把和它相干的ViewHolder存储起来,到用的时候再拿出来

convertView.setTag(vHolder);

}

else{

//若是不为null就直接经由过程getTag取出来

vHolder = (ViewHolder)convertView.getTag();

}

//然后给ViewHolder对象的每一项赋值 s

Drawable drawable1 = getResources().getDrawable(R.drawable.benchi1);

vHolder.image.setImageBitmap(drawable1, 20);

return convertView;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值