ImageView learning, imageview

Source: Internet
Author: User

ImageView learning, 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; imp Ort 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 Liu Nan on 2016/8/31 0031.22: 14 */public class NetImageView extends ImageView {Bitmap mBitmap;/*** whether it is a circle */private boolean isRound = false; p Ublic 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 );}}; /*** create a circular image * @ 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, TributeSet attrs) {super (context, attrs);}/*** sets the image of the local file * @ param url */public void setLocalImage (String url) {FileInputStream FCM = null; try {FCM = new FileInputStream (url); mBitmap = BitmapFactory. decodeStream (FS); mHandler. sendEmptyMessage (0x1324); // setImageBitmap (decodeStream);} catch (FileNotFoundException e) {e. printStackTrace ();} finally {if (FS! = Null) {try {FS. close ();} catch (IOException e) {e. printStackTrace () ;}}}/*** sets the resource file * @ 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 ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.