使用SeekBar更改图像透明度

时间:2017-04-23 11:36:14

标签: java android-studio

我必须做一个Android Studio项目,它必须使用SeekBar来改变ImageView的透明度。

我有这个程序:

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);

    }
}

我运行应用程序,但在我的智能手机上它不起作用,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这是搜索栏的xml

 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) {

        }

    });

这是活动的代码

npm install -g yarn
ng set --global packageManager=yarn

如果需要,您可以更改值