I have to do an Android Studio project that it has to change the transparency of an ImageView using a SeekBar.
I have this program:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int seekTransparent;
ImageView color;
SeekBar seekBarTransparent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
color = (ImageView)findViewById(R.id.changeColor);
seekBarTransparent = (SeekBar) findViewById(R.id.seekBar);
updateNow();
seekBarTransparent.setOnSeekBarChangeListener(seekBarChangeListener);
}
private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new
SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
updateNow();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "toast in uso",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "toast non in uso",
Toast.LENGTH_SHORT).show();
}
};
private void updateNow(){
seekTransparent = seekBarTransparent.getProgress();
color.getBackground().setAlpha(192 + seekTransparent * 0x10000);
}
}
I run the application but on my smartphone it doesn't work anyone can help me?
This is the xml of the seekbar
<SeekBar
android:id="@+id/opacitybar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:max="10"
android:progress="10"
android:layout_alignTop="@+id/opacitylbl"
android:layout_centerHorizontal="true" />
And this is the code of the Activity
logoseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int opacity = 100; // from 0 to 255
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
im_move_zoom_rotate.setAlpha(progresValue * 25);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can change the values if you want
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.