使用搜索栏更改 ImageView 上的图像?

Using seek bar to change Image at ImageView?

我正在学习实现方法,我有一个带有弹出菜单、图像视图和搜索栏的示例应用程序。 When an item in popup is selected, it displays 5 images which depend on the position of seekbar, i.e the images are changed when the seekbar is moved after an item is selected, i am stuck at the method implmentation, at onProgressChanged... ..

还有在case R.id.item之后添加什么代码来改变图片x1,x3,x6,x5,m6?

谢谢..

   public class MainActivity extends Activity implements  OnMenuItemClickListener {

     public ImageView image;

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

        findViewById(R.id.btn_click).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
                popupMenu.setOnMenuItemClickListener(MainActivity.this);
                popupMenu.inflate(cars);
                popupMenu.show();
            }
        });
    }

    public boolean onMenuItemClick(MenuItem item) {

        SeekBar sk = (SeekBar) findViewById(R.id.seek_sh);

        sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                                          @Override
                                          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                                          }
                                      }

        image = (ImageView) findViewById(R.id.imageView);
        switch (item.getItemId()) {
            case R.id.item_BMW:
                image.setImageResource(x1);
                return true;


        }
        return true;
    }
}

您可以在 onCreate 方法中添加 SeekBarChangeListener 并根据 progress 值更改图像:

    public class MainActivity extends Activity implements OnMenuItemClickListener {

        public ImageView image;
        public SeekBar sk;

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

            image = (ImageView) findViewById(R.id.imageView);
            sk = (SeekBar) findViewById(R.id.seek_sh);

            findViewById(R.id.btn_click).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
                    popupMenu.setOnMenuItemClickListener(MainActivity.this);
                    popupMenu.inflate(cars);
                    popupMenu.show();
                }
            });


        sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                   switch (progress) {

                      case progressValueForX1:
                           image.setImageResource(R.drawable.x1);
                      break;

                      case progressValueForX2:
                           image.setImageResource(R.drawable.x2);
                      break;

                      ....

                      case progressValueForM6:
                           image.setImageResource(R.drawable.m6);
                      break;

                   }
                }
            }
      }
   }