Android and Setting Alpha for (Image) View Alpha

Android and setting alpha for (image) view alpha

No, there is not, see how the "Related XML Attributes" section is missing in the ImageView.setAlpha(int) documentation. The alternative is to use View.setAlpha(float) whose XML counterpart is android:alpha. It takes a range of 0.0 to 1.0 instead of 0 to 255. Use it e.g. like

<ImageView android:alpha="0.4">

However, the latter in available only since API level 11.

How to change the Alpha of an ImageView in android

In XML use

android:alpha="" alpha between 0 to 1 

In code use

imageView.setAlpha(int);

Similar Question

How to Set Opacity (Alpha) for View in Android

I just found your question while having the similar problem with a TextView. I was able to solve it, by extending TextView and overriding onSetAlpha. Maybe you could try something similar with your button:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class AlphaTextView extends TextView {

public AlphaTextView(Context context) {
super(context);
}

public AlphaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onSetAlpha(int alpha) {
setTextColor(getTextColors().withAlpha(alpha));
setHintTextColor(getHintTextColors().withAlpha(alpha));
setLinkTextColor(getLinkTextColors().withAlpha(alpha));
return true;
}
}

Difference between setAlpha and setImageAlpha

ImageView.setAlpha(int) has been renamed to ImageView.setImageAlpha(int) to avoid confusion with the new method View.setAlpha(float) introduced in API level 11.

View.setAlpha(float) is a general method available on all Views, including ImageView. It applies the specified opacity to the whole view. To achieve this, by default the system creates a temporary buffer (a hardware layer) where the View is drawn as usual, then the buffer is drawn on the screen with the specified alpha value. It's a two-pass mechanism which requires the initial allocation of a buffer, so it's somewhat slower. See this video for more information and how to change the default behavior: Hidden Cost of Transparency.
It's important to note that ImageView includes by default an optimization that will avoid this buffer allocation if it has no background, so in practice there will be no performance penalty when calling ImageView.setAlpha(float) if the ImageView has no background.

ImageView.setImageAlpha(int) (and ImageView.setAlpha(int)) are methods proper to the ImageView. They control the alpha value which is used to draw the content image (bitmap or other) directly on the screen, with no intermediate pass, so this is the preferred method to use to apply transparency to an image displayed by an ImageView. Of course if you set a background Drawable on your ImageView that you also want to be translucent, this method will not produce the expected result.

how to change the alpha value of imageview

Maybe you can try like this, I had troubles doing it normal way but with "animation" it was ok.

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F); // change values as you want
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your imageview
yourImageView.startAnimation(alpha);

How to set image alpha back to normal

Don't like answering my own question, but I was able to fix this problem. Use cross.setAlpha(1f) instead of cross.setAlpha(255). I guess setAlpha takes a float form 0 to 1 instead of 0 to 255.

How to set alpha property to image not to text in Android Studio?

Try using FrameLayout like below code:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:layout_gravity="center"
android:background="@drawable/coffee" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Hello World!"
android:textColor="@color/orange"
android:textSize="30sp" />
</LinearLayout>

</FrameLayout>


Related Topics



Leave a reply



Submit