Change Opacity of ImageView programmatically

Example to change opacity/alpha of ImageView programmatically, by calling deprecated setAlpha(alpha) or setImageAlpha(alpha) for APL Level 16 or higher.


MainActivity.java
package com.example.androidsetimageopacity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

SeekBar barOpacity;
ImageView image;
TextView textOpacitySetting;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

image = (ImageView)findViewById(R.id.image);
textOpacitySetting = (TextView)findViewById(R.id.opacitysetting);
barOpacity = (SeekBar)findViewById(R.id.opacity);

int alpha = barOpacity.getProgress();
textOpacitySetting.setText(String.valueOf(alpha));
image.setAlpha(alpha); //deprecated
//image.setImageAlpha(alpha); //for API Level 16+

barOpacity.setOnSeekBarChangeListener(barOpacityOnSeekBarChangeListener);
}

OnSeekBarChangeListener barOpacityOnSeekBarChangeListener =
new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int alpha = barOpacity.getProgress();
textOpacitySetting.setText(String.valueOf(alpha));
image.setAlpha(alpha); //deprecated
//image.setImageAlpha(alpha); //for API Level 16+
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

};

}

activity_main.xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidsetimageopacity.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<SeekBar
android:id="@+id/opacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="511"
android:progress="100"/>
<TextView
android:id="@+id/opacitysetting"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

</LinearLayout>




1 Response to "Change Opacity of ImageView programmatically"

  1. thank you for sharing such article and i also want you to see the best happy valentines day 2020

    ReplyDelete