Angkor Empire Angkor Empire - 1 month ago 11
Android Question

Condition with xml property in java android

I have one question is it possible to check the condition with xml tribute or property.
Example
I have 2 picture: pict1, pict2
I want to check if pict1 have Alpha=0.5 then pict2 Alpha=1 else reverse it

Can any one tell me the solution.

note: the picture Alpha can be set by xml or set by java.

Answer

Not exactly sure what you're looking for as far as logic goes, but maybe this helps:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image_one"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:src="@drawable/image_two"/>

    <ImageView
        android:id="@+id/image_two"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:src="@drawable/image_two"/>

</LinearLayout>

In your Activity or Fragment's onCreate():

private static final int halfAlpha = 255 / 2;
private static final int opaque = 255;

ImageView imageOne = (ImageView) findViewById(R.id.image_one);
imageOne.setImageAlpha(halfAlpha);
ImageView imageTwo = (ImageView) findViewById(R.id.image_two);
imageTwo.setImageAlpha(opaque);

Then your conditional:

public void switchImageAlpha() {
    if (imageOne.getImageAlpha() == halfAlpha) {
        imageOne.setImageAlpha(opaque);
        imageTwo.setImageAlpha(halfAlpha);
    } else {
        imageOne.setImageAlpha(halfAlpha);
        imageTwo.setImageAlpha(opaque);
    }
}

Note: when setting image alpha programmatically the alpha is an int:

the alpha value that will be applied to the drawable of this ImageView (between 0 and 255 inclusive, with 0 being transparent and 255 being opaque)