ImageView學習,imageview

來源:互聯網
上載者:User

ImageView學習,imageview

 

package liu.roundimagedemo.view;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.Drawable;import android.os.Handler;import android.os.Message;import android.support.v4.graphics.drawable.RoundedBitmapDrawable;import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;import android.util.AttributeSet;import android.widget.ImageView;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;/** * Created by 劉楠 on 2016/8/31 0031.22:14 */public class NetImageView extends ImageView {    Bitmap mBitmap;    /**     * 是否為圓形     */    private boolean isRound = false;    public void setRound(boolean round) {        isRound = round;    }    Handler mHandler = new Handler(){        @Override        public void handleMessage(Message msg) {            //super.handleMessage(msg);           if(!isRound){               setImageBitmap(mBitmap);               return;           }            Drawable drawable =createRoundBitmap(mBitmap);            setImageDrawable(drawable);        }    };    /**     * 建立圓形圖片     * @param src     * @return     */    private Drawable createRoundBitmap(Bitmap src) {        Bitmap dst;        if(src.getWidth()>src.getHeight()){            dst = Bitmap.createBitmap(src,src.getWidth()/2-src.getHeight()/2,0,src.getHeight(),src.getHeight());        }else {            dst = Bitmap.createBitmap(src,0,src.getHeight()/2-src.getWidth()/2,src.getWidth(),src.getWidth());        }        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),dst);        roundedBitmapDrawable.setAntiAlias(true);        roundedBitmapDrawable.setCornerRadius(dst.getWidth()/2);        return roundedBitmapDrawable;    }    public NetImageView(Context context) {        super(context);    }    public NetImageView(Context context, AttributeSet attrs) {        super(context, attrs);    }    /**     * 設定本地檔案的圖片     * @param url     */    public void setLocalImage(String url){        FileInputStream fis =null;        try {            fis  = new FileInputStream(url);            mBitmap = BitmapFactory.decodeStream(fis);            mHandler.sendEmptyMessage(0x1324);            //setImageBitmap(decodeStream);        } catch (FileNotFoundException e) {            e.printStackTrace();        }finally {            if(fis!=null){                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    /**     * 設定資源檔     * @param resId     */    public void setResourceImage(int resId){       mBitmap = BitmapFactory.decodeResource(getResources(), resId);        mHandler.sendEmptyMessage(0x133);       // setImageBitmap(bitmap);    }    public void setUrlImage(final String url){       new Thread(){           @Override           public void run() {               try {                   URL uri = new URL(url);                   HttpURLConnection conn = (HttpURLConnection) uri.openConnection();                  // conn.setConnectTimeout(100);                  // conn.setDoInput(true);                  // conn.setReadTimeout(100);                   int responseCode = conn.getResponseCode();                   if(responseCode==200){                       InputStream is = conn.getInputStream();                       mBitmap = BitmapFactory.decodeStream(is);                       mHandler.sendEmptyMessage(0x343);                       is.close();                   }               } catch (MalformedURLException e) {                   e.printStackTrace();               } catch (IOException e) {                   e.printStackTrace();               }finally {               }           }       }.start();    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.