我认为,更好的方法是创建一个自定义的ImageView和重写的onDraw方法。喜欢的东西:
public class CustomView extends ImageView {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrst) {
super(context, attrst);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
MyBitmapFactory bitMapFac = null;
public void setBitmapFactory(MyBitmapFactory bitMapFac)
{
this.bitMapFac = bitMapFac;
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
/*instantiate a bitmap and draw stuff here, it could well be another
class which you systematically update via a different thread so that you can get a fresh updated
bitmap from, that you desire to be updated onto the custom ImageView.
That will happen everytime onDraw has received a call i.e. something like:*/
Bitmap myBitmap = bitMapFac.update(); //where update returns the most up to date Bitmap
//here you set the rectangles in which you want to draw the bitmap and pass the bitmap
canvas.drawBitmap(myBitMap, new Rect(0,0,400,400), new Rect(0,0,240,135) , null);
super.onDraw(canvas);
//you need to call postInvalidate so that the system knows that it should redraw your custom ImageView
this.postInvalidate();
}
}
这将是一个好主意,实施,检查是否有新的位图通过update()方法来获取一些逻辑,让里面的onDraw代码将不会执行每次和将系统开销。
然后使用您的自定义视图,无论你需要它。最简单的方法是直接在activity_layout.xml内声明它是这样:
<com.mycustomviews.CustomView
android:id="@+id/customView"
android:layout_centerInParent="true"
android:layout_height="135dp"
android:layout_width="240dp"
android:background="@android:color/transparent"/>
,然后访问是在你的代码像任何其他视图通过使用:
customView = (CustomView) findViewById(R.id.customView);
来源
2015-09-15 12:05:26
Lys
请更新的链接。博客被删除 – 2013-08-20 08:56:09