ClipDrawable 显示网络图片

1、ClipDrawable 显示网络图片

    /**
     * ClipDrawable图片逐渐显示的Drawable
     */
    private ImageView mIvPerson;
    private Thread mThread;
    private static MyHandler handler;
    static class MyHandler extends Handler {

        private WeakReference mWeakReference;

        public MyHandler(DetectAnalysisActivity activity) {
            mWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            DetectAnalysisActivity activity = mWeakReference.get();
            if (activity != null) {
                Drawable bitDrawable = (Drawable) msg.obj;
                ClipDrawable clipDrawable = new ClipDrawable(bitDrawable, Gravity.BOTTOM, ClipDrawable.VERTICAL);
                if (msg.what == 0) {
                    Log.d("DetectAnalysisActivity", "DetectAnalysisActivity3");
                    clipDrawable.setLevel(msg.what);
                    activity.mIvPerson.setImageDrawable(clipDrawable);
                    handler.sendEmptyMessageDelayed(1000, 60);
                } else {
                    Log.d("DetectAnalysisActivity", "DetectAnalysisActivity4");
                    if (clipDrawable.getLevel() <= 10000) {
                        clipDrawable.setLevel(msg.what);
                        handler.sendEmptyMessageDelayed(clipDrawable.getLevel() + 1000, 60);
                    } else {
                        //皮肤属性动画
                        activity.skinPropertiesAnim();
                        handler.removeMessages(msg.what);
                    }
                }
            }

        }
    }
    protected void initView() {
        mIvPerson = findViewById(R.id.iv_person);
        handler = new MyHandler(this);
    }

   /**
     * 动画效果
     */
    private void initAaim() {
        //中间图片动画
        String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561380985815&di=213ed854d27178a7a38f44dd0abfe85e&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F0338cbe93580d5e6b0e89f25531541d455f66fda4a6a5-eVWQaf_fw658";
        mThread = new MyThread(url, handler);
        mThread.start();
    }
    /**
     * 使用静态的线程防止内存泄露
     */
    public static class MyThread extends Thread {

        private String url;
        private Handler handler;

        public MyThread(String url, Handler handler) {
            this.url = url;
            this.handler = handler;
        }

        @Override
        public void run() {
            super.run();
            Log.d("DetectAnalysisActivity", "DetectAnalysisActivity1");
            Drawable bitDrawable = new BitmapDrawable(downloadUrlBitmap(url));
            Message msg = Message.obtain();
            msg.obj = bitDrawable;
            msg.what = 0;
            Log.d("DetectAnalysisActivity", "DetectAnalysisActivity2");
            handler.sendMessage(msg);
        }
    }

   /**
     * 根据Url下载图片bitmap
     *
     * @param urlString
     * @return
     */
    private static Bitmap downloadUrlBitmap(String urlString) {
        HttpURLConnection urlConnection = null;
        BufferedInputStream in = null;
        Bitmap bitmap = null;
        try {
            final URL url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
            bitmap = BitmapFactory.decodeStream(in);
        } catch (final IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            try {
                if (in != null) {
                    in.close();
                }
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }
        

2、ClipDrawable 显示本地图片

package com.hzy.exampledemo.ui.drawable;

import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

import com.hzy.exampledemo.R;

import java.lang.ref.WeakReference;

/**
 * Description ClipDrawableActivity
 *
 * @author hzy
 * Create on 2019/7/12 11:27
 */
public class ClipDrawableActivity extends AppCompatActivity {

    private ImageView mIvDmhs;

    /**
     * ClipDrawable图片逐渐显示的Drawable
     */
    private ClipDrawable clipDrawable;
    private Handler mHandler;

    static class MyHandler extends Handler {

        WeakReference mWeakReference;

        public MyHandler(ClipDrawableActivity activity) {
            mWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d("ClipDrawable-what", msg.what + "");
            ClipDrawableActivity activity = mWeakReference.get();
            if (null != activity) {
                if (msg.what == 0) {
                    Log.d("ClipDrawableActivity", "ClipDrawableActivity1");
                    activity.clipDrawable.setLevel(msg.what);
                    activity.mHandler.sendEmptyMessageDelayed(1000, 60);
                } else {
                    if (activity.clipDrawable.getLevel() < 10000) {
                        Log.d("ClipDrawableActivity", "ClipDrawableActivity2");
                        activity.clipDrawable.setLevel(msg.what);
                        activity.mHandler.sendEmptyMessageDelayed(activity.clipDrawable.getLevel() + 1000, 60);
                    } else {
                        Log.d("ClipDrawableActivity", "ClipDrawableActivity3");
                    }
                }
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clip_drawable);
        mIvDmhs = findViewById(R.id.iv_dmhs);
        mHandler = new MyHandler(this);
        clipDrawable = (ClipDrawable) mIvDmhs.getDrawable();
        clipDrawable.setLevel(0);
        mHandler.sendEmptyMessage(0);
    }
}




    








你可能感兴趣的:(ClipDrawable 显示网络图片)