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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <!-- <com.lian.image.SetImage
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> -->
 
    <ListView
        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" />
 
</LinearLayout>
 
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<HashMap<String,Object>> 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<HashMap<String,Object>> getData(){        
         ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
            HashMap<String,Object> map = new HashMap<String,Object>();
               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<HashMap<String,Object>> arrayList;
        //要绑定的资料文件id
         int resID;                                  
        public MyAdapter(Context context,
                ArrayList<HashMap<String, Object>> 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;
        }
   }
} 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:java合并文本文件并删除文件中重复行

下一篇:python中map的用法