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 );