zoukankan      html  css  js  c++  java
  • 一起学习android图片四舍五入图片集资源 (28)

    效果图:







    參看下面代码:

    public class MainActivity extends Activity {
    	private ImageView imageView1;
    	private ImageView imageView2;
    	Bitmap mBitmap;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.image);
    		initView();
    	
    	}
    
    	private void initView(){
    		imageView1=(ImageView)findViewById(R.id.imageView1);
    		imageView2=(ImageView)findViewById(R.id.imageView2);
    		//读取资源图片
    		mBitmap=readBitMap();
    		//对资源图片进行缩放
    		Bitmap bitmap=zoomBitmap(mBitmap, mBitmap.getWidth()/2, mBitmap.getHeight()/2);
    		//设置圆角图片
    		imageView2.setImageBitmap(setRoundedCorner(bitmap,20f));
    	}
    	
    	
    	/**
    	 * 读取资源图片
    	 * @return 
    	 */
    	private Bitmap readBitMap(){
    		BitmapFactory.Options opt=new BitmapFactory.Options();
    		/*
    		 * 设置让解码器以最佳方式解码
    		 */
    		opt.inPreferredConfig=Bitmap.Config.RGB_565;
    		//以下两个字段须要组合使用
    		opt.inPurgeable=true;
    		opt.inInputShareable=true;
    		/*
    		 * 获取资源图片
    		 */
    		InputStream is=this.getResources().openRawResource(R.drawable.mei);
    		return BitmapFactory.decodeStream(is, null, opt);
    	}
    
    	
    	/**
    	 * 缩放图片
    	 * @param bitmap
    	 * @param w
    	 * @param h
    	 * @return
    	 */
    	public  Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    		int width = bitmap.getWidth();
    		int height = bitmap.getHeight();
    		Matrix matrix = new Matrix();
    		float scaleWidht = ((float) w / width);
    		float scaleHeight = ((float) h / height);
    		/*
    		 * 通过Matrix类的postScale方法进行缩放
    		 */
    		matrix.postScale(scaleWidht, scaleHeight);
    		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    		return newbmp;
    	}
    	
    	/**
    	 * 设置图片为圆角
    	 * @param bitmap
    	 * @param roundPx  圆角角度
    	 * @return
    	 */
    	public  Bitmap setRoundedCorner(Bitmap bitmap, float roundPx) {
    		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    		Canvas canvas = new Canvas(output);
    
    		final Paint paint = new Paint();
    		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    		/*
    		 * 椭圆形
    		 */
    		final RectF rectF = new RectF(rect);
    		/*
    		 * 去锯齿
    		 */
    		paint.setAntiAlias(true);
    		canvas.drawARGB(0, 0, 0, 0);
    
    		/*
    		 * 绘制圆角矩形
    		 */
    		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    		/*
    		 * 设置两个图形相交
    		 */
    		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    		canvas.drawBitmap(bitmap, rect, rect, paint);
    
    		return output;
    	}
    	
    }


    转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/44314043 情绪控制_

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    [工作积累] shadow map问题汇总
    引擎设计跟踪(九.14.3.4) mile stone 2
    引擎设计跟踪(九.14.3.3) Deferred shading的一些小细节
    引擎设计跟踪(九.14.3.2) Deferred shading的后续实现和优化
    《口袋妖怪 太阳/月亮》正式公布 简体中文确认
    古墓丽影:崛起 PC版今日发售
    枪弹辩驳(弹丸论破)即将登陆PC
    引擎设计跟踪(九.14.3.1) deferred shading: Depthstencil as GBuffer depth
    引擎设计跟踪(九.14.3) deferred shading 准备
    引擎设计跟踪(九.14.2 final) Inverse Kinematics: CCD 在Blade中的实现
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4862260.html
Copyright © 2011-2022 走看看