Reputation: 801
I am using the following code to set the alpha value of an ImageView (this should be compatible with all devices, even pre API 11)
AlphaAnimation alpha = new AlphaAnimation(0.85F, 0.85F);
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
ImageView view = (ImageView) findViewById(R.id.transparentBackground);
view.startAnimation(alpha);
However, when I open the app on devices running on gingerbread and below, the imageView is completely transparent but on devices running on honeycomb or higher, the alpha value is set to .85 and the imageView is displayed perfectly.
How can make this happen on gingerBread as well?
Upvotes: 1
Views: 5314
Reputation: 4142
An easy way to get this to work is to use the NineOldAndroids project which ports the Honeycomb animation API back to older versions including AlphaAnimation. See http://nineoldandroids.com/.
So, using the ObjectAnimator would be something like this:
ObjectAnimator.ofFloat(imageView, "alpha", .85f).start();
Upvotes: 2
Reputation: 12719
Ok, lets create an custom imageview, it solves the problem , by overriding the onDraw :)
AlphaImageView.java
package com.example.shareintent;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AlphaImageView extends ImageView {
public AlphaImageView(Context context) {
super(context);
}
public AlphaImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlphaImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas){
canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 0x66, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
super.onDraw(canvas);
}
}
your activity xml
<com.example.shareintent.AlphaImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="52dp"
android:layout_marginTop="50dp"
android:src="@drawable/ic_launcher" />
Upvotes: 0
Reputation: 28541
You have a function to do it (has been deprecated because of the new view's generic way of handling transparency but can be used safely on Android 2.x):
myImageView.setAlpha(128); // range [0, 255]
You'll have to implement a custom animation though. This can be done with a handler for instance:
Handler animationHandler = new Handler() {
public void handleMessage(Message msg) {
int currentAlpha = msg.arg1;
int targetAlpha = msg.arg2;
if (currentAlpha==targetAlpha) return;
else {
if (currentAlpha<targetAlpha) --currentAlpha;
else ++currentAlpha;
imageView.setAlpha(currentAlpha);
sendMessageDelayed(obtainMessage(0, currentAlpha, targetAlpha), 10);
}
}
}
// Show imageview
animationHandler.sendMessage(animationHandler.obtainMessage(0, currentAlpha, 255));
// Hide imageview
animationHandler.sendMessage(animationHandler.obtainMessage(0, currentAlpha, 0));
Upvotes: 0