• 自定义圆形工具类


    1.自定义圆形工具类

    CircleImageViewUtil.java

    关键点:可以自持缩放

    package com.example.circleimageviewdemo;
    
    import android.content.Context;
    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.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    /**
     * 圆形的Imageview
     * 
     * @author tf
     * 
     */
    public class CircleImageViewUtil extends ImageView {
    	public CircleImageViewUtil(Context context) {
    		this(context, null);
    	}
    
    	public CircleImageViewUtil(Context context, AttributeSet attrs) {
    		this(context, attrs, 0);
    	}
    
    	public CircleImageViewUtil(Context context, AttributeSet attrs, int defStyle) {
    		super(context, attrs, defStyle);
    
    	}
    
    	@Override
    	protected void onDraw(Canvas canvas) {
    
    		Drawable drawable = getDrawable();
    
    		if (drawable == null) {
    			return;
    		}
    
    		if (getWidth() == 0 || getHeight() == 0) {
    			return;
    		}
    
    		Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    
    		if (null == b) {
    			return;
    		}
    
    		Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
    
    		int w = getWidth(), h = getHeight();
    
    		Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    		canvas.drawBitmap(roundBitmap, 0, 0, null);
    
    	}
    
    	public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    		Bitmap sbmp;
    		if (bmp.getWidth() != radius || bmp.getHeight() != radius)
    			sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    		else
    			sbmp = bmp;
    		Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
    				Config.ARGB_8888);
    		Canvas canvas = new Canvas(output);
    
    		final int color = 0xffa19774;
    		final Paint paint = new Paint();
    		final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
    
    		paint.setAntiAlias(true);
    		paint.setFilterBitmap(true);
    		paint.setDither(true);
    		canvas.drawARGB(0, 0, 0, 0);
    		paint.setColor(Color.parseColor("#BAB399"));
    		canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
    				sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    		canvas.drawBitmap(sbmp, rect, rect, paint);
    
    		return output;
    	}
    }
    

    2、MainActivity.java

    package com.example.circleimageviewdemo;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URL;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    	CircleImageViewUtil circle_imageview ;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_circle_image_view);
    		circle_imageview = (CircleImageViewUtil) findViewById(R.id.circle_imageview);
    	
    		circle_imageview.setImageResource(R.drawable.guide3);
    		//circle_imageview.setImageBitmap(bm);
    		//circle_imageview.setImageDrawable(drawable)
    		/*Uri uri = new Uri("");
    		circle_imageview.setImageURI(uri);*/
    		
    	
    
    	}
    
    	
    
    }
    

    xml文件:

    关键点: <com.example.circleimageviewdemo.CircleImageViewUtil>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        <com.example.circleimageviewdemo.CircleImageViewUtil
            android:layout_height="80dp"
            android:layout_width="80dp"
            android:id="@+id/circle_imageview"
            >
        </com.example.circleimageviewdemo.CircleImageViewUtil>
        
      
    
    </RelativeLayout>
    
  • 相关阅读:
    CMMI集谈
    镜像
    屌丝giser成长记-研一篇(上)
    天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十二)水情雨情模块
    天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十)态势标绘模块
    天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十三)台风模块
    天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十一)路径导航模块
    天津政府应急系统之GIS一张图(arcgis api for flex)讲解(九)地图定位模块
    天津政府应急系统之GIS一张图(arcgis api for flex)讲解(八)资源搜索模块
    天津政府应急系统之GIS一张图(arcgis api for flex)讲解(七)地图打印模块
  • 原文地址:https://www.cnblogs.com/childhooding/p/4461723.html
Copyright © 2020-2023  润新知