SimpleAdapter uses the Drawable and Bitmap object methods, bitmap to drawable

Source: Internet
Author: User

SimpleAdapter uses the Drawable and Bitmap object methods, bitmap to drawable

Methods for using Drawable and Bitmap objects in SimpleAdapter

When we usually use SimpleAdapter as the ListView or GridView adapter, if we want to display an image, we usually use the image id R. drawable. xxx to bind the image to the view.

However, if the image we want to use is a Drawable or Bitmap object, the above method cannot solve the problem. The following describes how to use a Drawable or Bitmap object in SimpleAdapter.

1. Use the Drawable object as the image resource to be adapted by SimpleAdapter

SimpleAdapter adapter =NewSimpleAdapter (This, Items, R. layout.Line,NewString [] {"imgIco", "appName", "packageName "},New int[] {R. id.ImgIco, R. id.TvAppName, R. id.TvAppDesc});

List. setAdapter (adapter );

// ViewBinder can help SimpleAdapter load images (such as Bitmap and Drawable)

Adapter. setViewBinder (NewViewBinder (){

@ Override

PublicbooleanSetViewValue (View view, Object data,

String textRepresentation ){

//TODOAuto-generated method stub

If(ViewInstanceofImageView & dataInstanceofDrawable ){

ImageView iv = (ImageView) view;

Iv. setImageDrawable (Drawable) data );

Returntrue;

}Else{

Returnfalse;

}

}

});

2. Use the Bitmap object as the image resource to be adapted by SimpleAdapter

SimpleAdapter adapter =NewSimpleAdapter (This, Items, R. layout.Line,NewString [] {"imgIco", "appName", "packageName "},New int[] {R. id.ImgIco, R. id.TvAppName, R. id.TvAppDesc});

List. setAdapter (adapter );

// ViewBinder can help SimpleAdapter load images (such as Bitmap and Drawable)

Adapter. setViewBinder (NewViewBinder (){

@ Override

PublicbooleanSetViewValue (View view, Object data,

String textRepresentation ){

//TODOAuto-generated method stub

If(ViewInstanceofImageView & dataInstanceofBitmap ){

ImageView iv = (ImageView) view;

Iv. setImageBitmap (Bitmap) data );

Returntrue;

}Else{

Returnfalse;}

}

});

The general process of the above method is as follows: First, check whether SimpleAdapter has specified SimpleAdapter. ViewBinder. If so, call its setViewValue method.

SimpleAdapter. viewBinder is an interface and only has this method. If ViewBinder returns true, it means that we have bound the data of this View, so we will not call the system default implementation, of course, we can also set a ViewBinder to add some functions, and then bind the system with data by returning false. For example, to add a response event to the button, the text on the button is bound by default.


How to copy a bitmap from a drawable object

[Mw_shl_code = java, true] Bitmap srcBmp = (BitmapDrawable) drawable ). getBitmap (); Bitmap destBmp = srcBmp. copy (srcBmp. getConfig (), true); // The returned bitmap is copied and released externally without affecting the drawable object LogUtil in the cache layer. log (TAG, srcBmp + "=" + destBmp); [/mw_shl_code] To get the truly Copied object. I don't know if you have any other methods.

How does android convert a drawable object to a canvas object?

Drawable mDrawable = this. getResources (). getDrawable (R. drawable. bgd01 );
Bitmap mBitmap = (BitmapDrawable) mDrawable). getBitmap ();
Paint mPaint = new Paint ();
Canvas. drawBitmap (mBitmap, 0, 0, mPaint );

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.