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>
3 comments:
How about if you want to change the opacity using a certain color, say color red.?
Hello Aaron Rono,
I'm not sure what you means the "opacity using a certain color", in my understanding, opacity (Alpha) apply on all three color, RGB.
Here is a example to change the opacity of ImageView and its background color of red, hope can help.
nice bt i want to do this for multiple stickers...what i do for that
Post a Comment