Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing translucent bitmaps using Canvas (Android)

Tags:

java

android

I have a Bitmap object and want to render it to a Canvas object with varying levels of translucency (i.e. make the whole bitmap partially see through). For example, I have sprites in a game (that are drawn over the top of a bitmap background) that I want to fade out from being opaque to being invisible. Can I do this without having to resort to OpenGL?

like image 644
wordyword Avatar asked Mar 17 '10 23:03

wordyword


People also ask

How do I make bitmap transparent in Android?

Using adjustOpacity , I make ImageView 's Bitmap be semi-transparent. Bitmap newBitmap = adjustOpacity(orignalBitmap, 10); view. setImageBitmap(newBitmap); view. setBackgroundColor(Color.

How do you make a bitmap on Canvas?

Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) . Set dst to the size of the rectangle you want the entire image to be scaled into. EDIT: Here's a possible implementation for drawing the bitmaps in squares across on the canvas.

Can we draw directly on Canvas in android studio?

The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).

How do you draw a bitmap?

To draw on a bitmap, use the image control's canvas and attach the mouse-event handlers to the appropriate events in the image control. Typically, you would use region operations (fills, rectangles, polylines, and so on). These are fast and efficient methods of drawing.


1 Answers

You should be able to define a tween animation and apply that animation to an imageView. The XML resource would be similar to the following (named fade_animation.xml):

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator"
   android:fromAlpha="1.0"
   android:toAlpha="0.0"
   android:duration="100" />

then you would load and apply this animation to your custom imageView when ready:

ImageView sprite = (ImageView) findViewById(R.id.sprite);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_animation);
sprite.startAnimation(animation);

There are other options too. If you are using bitmaps and you don't want to do an animation, then you can manually decrease the alpha value of each frame, somewhat like this:

paint.setAlpha(100);
canvas.drawBitmap(spriteImage, left, top, paint);

Did you try any of these options?

like image 87
YGL Avatar answered Nov 15 '22 18:11

YGL