Android--自定义View加载网络图片,实现缩放,圆形剪裁
来源:互联网 发布:mac系统电脑玩英雄联盟 编辑:程序博客网 时间:2024/05/22 00:20
功能:加载网络图片,本地资源图片,圆形剪裁,图片自动缩放
package com.example.administrator.myapplication;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Rect;import android.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.text.TextUtils;import android.util.AttributeSet;import android.view.View;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * Created by Administrator on 2017/6/28. */public class HeadView extends View { private static final int SUCCESS = 101; private static final int FAILED = 102; private String fileName; private Bitmap img; private boolean makeCircle = false; private Paint paint = new Paint(); private Rect src = new Rect(); private Rect dst = new Rect(); private Path path = new Path(); private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SUCCESS: invalidate(); if (loadListener != null) { loadListener.onSuccess(); } saveBitmap(fileName); break; case FAILED: L.e("加载失败!"); if (loadListener != null) { loadListener.onFailed(); } break; default:break; } } }; public HeadView(Context context) { super(context); init(); } public HeadView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public void init() { paint.setAntiAlias(true); paint.setStrokeWidth(40); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int w,h; if (widthMode == MeasureSpec.EXACTLY) { w = width; } else { w = 200; } if (heightMode == MeasureSpec.EXACTLY) { h = height; } else { h = 200; } setMeasuredDimension(w,h); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (img != null) { int width = getWidth(); int height = getHeight(); int w = img.getWidth(); int h = img.getHeight(); if (makeCircle) { int x = width >> 1; int y = height >> 1; int radius = (width < height? width : height) >> 1; path.addCircle(x, y, radius, Path.Direction.CW); canvas.clipPath(path); dst.set(x -radius, y - radius, x + radius, y + radius); radius = (w < h? w : h) >> 1; x = w >> 1; y = h >> 1; src.set(x -radius, y - radius, x + radius , y + radius); canvas.drawBitmap(img, src, dst, paint); return; } float scale; if (w < width && h < height) { scale = 1.1f; while (w * scale < width && h * scale < height) { w *= scale; h *= scale; } } else { scale = 0.9f; while (w > width || h > height) { w *= scale; h *= scale; } } int left = (width - w) >> 1; int top = (height - h) >> 1; int right = width - left; int bottom = height - top; dst.set(left, top, right, bottom); canvas.drawBitmap(img, null, dst, paint); } } @Override protected void onDetachedFromWindow() { L.e("onDetachedFromWindow start"); if (img != null) { img.recycle(); } if (handler != null) { handler.removeCallbacksAndMessages(null); } L.e("onDetachedFromWindow end"); super.onDetachedFromWindow(); } public void setImageFromNet(final String url) { fixedThreadPool.execute(new Runnable() { @Override public void run() { InputStream bis = null; ByteArrayOutputStream bos = null; try { URL address = new URL(url); HttpURLConnection conn = (HttpURLConnection) address.openConnection(); conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode == 200) { bis = conn.getInputStream(); bos = new ByteArrayOutputStream(); int len; byte[] array = new byte[4096]; while ((len = bis.read(array)) != - 1) { bos.write(array, 0, len); } fileName = subUrlForName(url); byte[] data = bos.toByteArray(); img = BitmapFactory.decodeByteArray(data, 0, data.length); conn.disconnect(); bis.close(); bos.close(); handler.sendEmptyMessage(SUCCESS); } } catch (Exception e) { e.printStackTrace(); handler.sendEmptyMessage(FAILED); if (bis != null) { try { bis.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }); } public void saveBitmap(String name) { FileOutputStream fos = null; if (img != null) { try { fos = new FileOutputStream(getContext().getFileStreamPath(name)); img.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.close(); } catch (Exception e) { e.printStackTrace(); if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } } private String subUrlForName(String url) { if (!TextUtils.isEmpty(url)) { return url.substring(url.lastIndexOf("/")+1, url.length()); } else { return ""; } } public void setImage(String url) { if (!TextUtils.isEmpty(url)) { String name = subUrlForName(url); File file = getContext().getFileStreamPath(name); if (file.exists()) { L.e("second cache"); if (img != null) { img.recycle(); } img = BitmapFactory.decodeFile(getContext().getFileStreamPath(name).getAbsolutePath()); invalidate(); handler.sendEmptyMessage(SUCCESS); } else { L.e("third cache"); setImageFromNet(url); } } } public void setImg(int id) { img = BitmapFactory.decodeResource(getResources(), id); invalidate(); } public void setCircleImage() { makeCircle = true; path = new Path(); invalidate(); } private LoadListener loadListener; public interface LoadListener { void onSuccess(); void onFailed(); } public void setLoadListener(LoadListener loadListener) { this.loadListener = loadListener; }}
阅读全文
0 0
- Android--自定义View加载网络图片,实现缩放,圆形剪裁
- 【Android自定义View实战】之自定义圆形头像CircleImageView支持加载网络图片
- 自定义View实现Android圆形图片
- 自定义view实现圆形加载
- Android 自定义View ProgressBarCircle,圆形进度条,仿微博图片加载
- Android自定义view圆形图片
- Android 网络请求的图片用自定义圆形View展示
- 自定义View实现图片缩放
- Android自定义View实现不断旋转的圆形图片
- android加载网络图片,圆形图片加载
- android自定义View实现图片的绘制、旋转、缩放
- 自定义view实现圆形加载效果
- android 自定义View加载圆形进度条
- android 自定义图片剪裁
- 通过自定义View实现圆形图片
- 圆形图片的实现自定义view
- 实现自定义图片View圆形、圆角,椭圆
- android 自定义圆形图片实现
- <NOIP> 8 . P1035 级数求和
- 第六章 彩色图像处理
- space-vim IDE 编辑器
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- 2017-06-30 oracle MERGE INTO 单表查询
- Android--自定义View加载网络图片,实现缩放,圆形剪裁
- 实用的一些倒排索引代码
- MySQL 中 delete 语句的子查询限制
- javaWeb实战教程0-环境配置
- 一.玩转图灵机器人
- javaWeb实战教程1-servlet
- win7下jdk环境配置
- 线性布局 相对布局 属性介绍
- javaWeb实战教程2-servlet内置对象和错误处理