Je pense qu'une meilleure approche serait de créer un ImageView personnalisé et Substituez la méthode onDraw.Quelque chose comme:
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();
}
}
Ce serait une bonne idée de mettre en œuvre une logique qui vérifie s'il y a un bitmap frais d'acquisition par la méthode mise à jour(), de sorte que le code à l'intérieur onDraw ne sera pas exécuté à chaque fois et mettre les frais généraux au système.
Et puis utilisez votre vue personnalisée où vous en avez besoin. La façon la plus simple serait de déclarer directement à l'intérieur du activity_layout.xml comme tel:
<com.mycustomviews.CustomView
android:id="@+id/customView"
android:layout_centerInParent="true"
android:layout_height="135dp"
android:layout_width="240dp"
android:background="@android:color/transparent"/>
Et l'accès est dans votre code comme tout autre point de vue en utilisant:
customView = (CustomView) findViewById(R.id.customView);
S'il vous plaît mettre à jour le lien. Blog est supprimé –