Question

i am confused to draw text after completion of animation here i tried i get draw able Drawable temp = animationDrawable.getCurrent(); after complete animation and send to method setDrawble(Drawable temp)
here i am writing text on last frame and return to imagview and set imagedrawable.

suggest other way also

here what i did i am posting my code

    ImageView imageview;
AnimationDrawable animationDrawable;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_keyframe_animation);

    imageview = (ImageView) findViewById(R.id.imageview);

    // Create the AnimationDrawable in which we will store all frames of the
    // animation
    animationDrawable = new AnimationDrawable();

    animationDrawable.addFrame(
            getResources().getDrawable(R.drawable.bingo_nw1), 0);

    // Run until we say stop
    animationDrawable.setOneShot(false);

    imageview.setImageDrawable(animationDrawable);

    imageview.setOnTouchListener(this);

}

protected void setBack() {
    // TODO Auto-generated method stub
    int num = 18;
    Random rand = new Random();
    int ran = rand.nextInt(num) + 9;

}

@Override
public boolean onTouch(View arg0, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        animationDrawable = new AnimationDrawable();

        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw2), 200);
        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw3), 200);
        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw4), 200);
        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw5), 200);
        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw6), 200);
        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw7), 200);
        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw8), 200);
        animationDrawable.addFrame(
                getResources().getDrawable(R.drawable.bingo_nw9), 200);

        imageview.setImageDrawable(animationDrawable);
        animationDrawable.start();

        Handler mHandler = new Handler();

        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                Drawable temp = animationDrawable.getCurrent();

                imageview.setImageDrawable(setDrawble(temp));
            }

        }, 4000);
        return true;
    }
    return false;
}

@SuppressWarnings("deprecation")
public Drawable setDrawble(Drawable temp) {

    Bitmap bm = Bitmap.createBitmap(temp.getIntrinsicWidth(),
            temp.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bm);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);
    paint.setTextSize(80);// Text Color
    paint.setStrokeWidth(12); // Text Size
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text
                                                                            // Overlapping
                                                                            // Pattern
    // some more settings...

    canvas.drawBitmap(bm, 0, 0, paint);
    canvas.drawText("Testing...", 10, 10, paint);

    temp.setBounds(100, 100, canvas.getWidth(), canvas.getHeight());
    temp.draw(canvas);

    return temp;
}
Was it helpful?

Solution

replace your handler like this

animationDrawable.start();
long totalDuration = 0;  
for(int i = 0; i< animation.getNumberOfFrames();i++){  
     totalDuration += animation.getDuration(i);  
}  


 Handler mHandler = new Handler();
 mHandler.postDelayed(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

        Drawable temp = animationDrawable.getCurrent();

        imageview.setImageBitmap(setDrawble(temp));
    }

}, totalDuration);

    public Bitmap setDrawble(Drawable temp) {

    Bitmap bm = Bitmap.createBitmap(temp.getIntrinsicWidth(), temp.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bm);
    temp.setBounds(0, 0, bm.getWidth(), bm.getHeight());
    temp.draw(canvas);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.CYAN);
    paint.setTextSize((int)(temp.getIntrinsicHeight()*0.3));// Text Color
    paint.setStrokeWidth(12); // Text Size
    canvas.drawText("Testing...", bm.getWidth() / 2, bm.getHeight() / 2,
            paint);//change x and y position where you want in the image
    return bm;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top