android如何用seekbar移动图像?

时间:2014-01-20 13:25:35

标签: android

我正在使用图库来自服务器的图库。我使用了一个随图像移动的搜索栏。问题是,当我移动搜索栏时,我的图像没有移动,也没有根据搜索条计数显示。我该怎么办?我正在寻找这个2天。请帮帮我,谢谢。

public class ReaderScreen extends Activity implements IServerResponse{

private Gallery gallery =null;
private SeekBar mybar =null;
ArrayList<String> _issueList= new ArrayList<String>();
private Gallery imagesComics = null;
private JSONArray post;
String _coverImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.reader_screen);

    mybar = (SeekBar) findViewById(R.id.seekBar1);
    gallery=(Gallery) findViewById(R.id.gallery);
    //  imagesComics =(Gallery) findViewById(R.id.images_comic);



    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View v,
                int position, long id) {
            //      mySelection.setText(" selected option: " + position );
            //  for(int i =0;i<post.length();i++){
            _coverImage=_issueList.get(position);
            mybar.setProgress(position);


            //  }

        }

        public void onNothingSelected(AdapterView<?> parent) {
            //  mySelection.setText("Nothing selected");

        }


    });


    mybar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            int seekValue = mybar.getProgress();

            Toast.makeText(ReaderScreen.this,""+seekValue, Toast.LENGTH_LONG).show();
            //  seekBar.setProgress(post.length());
            seekBar.setMax(post.length());


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            int seekValue = mybar.getProgress();
            //   _issueList.set(seekValue, _coverImage);
            Toast.makeText(ReaderScreen.this,""+seekValue, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            //   Toast.makeText(ReaderScreen.this,"onStopTrackingTouch", Toast.LENGTH_LONG).show();

        }
    });

    issues_images_WS(); 
}

private Handler _handler = new Handler() {
    public void handleMessage(Message msg) {
        String respons=msg.obj.toString();
        switch (msg.arg1) {
        case Constant.PID_ISSUES_IMAGES:
            if(respons!=null){
                try{
                    JSONObject jsonObj=new JSONObject(respons);
                    /*JSONArray*/ post=jsonObj.getJSONArray("List of image URLs");
                    for(int i =0;i<post.length();i++){
                        String values = post.getString(i);
                        _issueList.add(values);
                    }
                    gallery.setAdapter(new ImageAdapter(ReaderScreen.this,_issueList));
                }catch (JSONException e) {
                }
            }
            break;
        default:
            break;
        }
    }
};
// SERVER RESPONSE METHOD
public void serverResponse(String response, int processid) {
    Message msg = new Message();
    msg.arg1 = processid;
    msg.obj = response;
    _handler.sendMessage(msg);
}

public void issues_images_WS() {
    if (NetworkAvailablity.checkNetworkStatus(ReaderScreen.this)) {
        ArrayList<NameValuePair> requestParaList = new ArrayList<NameValuePair>();
        requestParaList.add(new BasicNameValuePair("user_id", Constant.USER_ID));
        requestParaList.add(new BasicNameValuePair("issue_id", "1"));

        WebServiceCommunicator.getInstance().registerForServerResponse(
                (IServerResponse) ReaderScreen.this);
        WebServiceCommunicator.getInstance().callGetAppWebService(
                Constant.showDialog, this, WebServiceConstants.getMethodUrl(WebServiceConstants.METHOD_NAME_GET_ISSUES_IMAGES),
                ReaderScreen.this, Constant.PID_ISSUES_IMAGES, false,
                requestParaList);
    } else {
        Constant.showAlertDialog(Constant.errorTitle,
                Constant.MSG_CHECK_INTERNET_SETTING,
                ReaderScreen.this, false);

    }
}

public class ImageAdapter extends BaseAdapter {
    /** The parent context */
    private Context myContext;
    ArrayList<String> _issueList1= new ArrayList<String>();
    private UserImageLoaderWithCache cache=null;

    // Put some images to project-folder: /res/drawable/
    // format: jpg, gif, png, bmp, ...
    /*private int[] myImageIds = { R.drawable.ic_launcher, R.drawable.ic_launcher,
                   R.drawable.ic_launcher, R.drawable.ic_launcher };*/

    /** Simple Constructor saving the 'parent' context. */
    public ImageAdapter(Context c,ArrayList<String> list) {
        this.myContext = c;
        this._issueList1 = list;
        cache=new UserImageLoaderWithCache(c);
    }
    private class ViewHolder{

        public ImageView issuesimages;

    }
    // inherited abstract methods - must be implemented
    // Returns count of images, and individual IDs
    public int getCount() {
        return this._issueList1.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    // Returns a new ImageView to be displayed,
    public View getView(int position, View convertView, 
            ViewGroup parent) {



        View v = convertView;
        final ViewHolder viewHolder;

        if(v==null){
            viewHolder=new ViewHolder();
            LayoutInflater linf = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = linf.inflate(R.layout.imageview, null);
            viewHolder.issuesimages=(ImageView)v.findViewById(R.id.image_view_comic);
            v.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) v.getTag();
        }
        String imageUrl=_issueList1.get(position);
        viewHolder.issuesimages.setTag(imageUrl);
        cache.display(imageUrl,viewHolder.issuesimages, R.drawable.loading);
        return v;

    }
}

}

enter image description here

2 个答案:

答案 0 :(得分:0)

如果您只想更改图像而不是图库,则应使用图像视图

@Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            int seekValue = mybar.getProgress();

            Toast.makeText(ReaderScreen.this, "" + seekValue,
                    Toast.LENGTH_LONG).show();
            // seekBar.setProgress(post.length());
            seekBar.setMax(5);
            gallery.setImageResource(_issueList.get(seekValue));

        }

gallery.setImageResource(_issueList.get(seekValue)) 应该相应地改变你应该在_issueList

中有图像
_issueList.add(R.drawable.ic_launcher);
    _issueList.add(R.drawable.ic_launcher);
    _issueList.add(R.drawable.ic_launcher);
    _issueList.add(R.drawable.ic_launcher);
    _issueList.add(R.drawable.ic_launcher);

答案 1 :(得分:0)

<ImageView
    android:id="@+id/gallery"
    android:layout_width="100dip"
    android:layout_height="100dip" />
<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="5" />

这是你的xml

public class ReaderScreen extends Activity {

private ImageView gallery = null;
private SeekBar mybar = null;
ArrayList<Integer> _issueList = new ArrayList<Integer>();
private Gallery imagesComics = null;
private JSONArray post;
String _coverImage;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.reader_screen);
    _issueList.add(R.drawable.ic_launcher);
    _issueList.add(R.drawable.device_one);
    _issueList.add(R.drawable.device_four);
    _issueList.add(R.drawable.device_therre);
    _issueList.add(R.drawable.device_two);
    mybar = (SeekBar) findViewById(R.id.seekBar1);
    gallery = (ImageView) findViewById(R.id.gallery);
    // imagesComics =(Gallery) findViewById(R.id.images_comic);

    /*gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View v,
                int position, long id) {
            // mySelection.setText(" selected option: " + position );
            // for(int i =0;i<post.length();i++){
        //  _coverImage = _issueList.get(position);
            mybar.setProgress(position);

            // }

        }

        public void onNothingSelected(AdapterView<?> parent) {
            // mySelection.setText("Nothing selected");

        }

    });*/

    mybar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            int seekValue = mybar.getProgress();

            Toast.makeText(ReaderScreen.this, "" + seekValue,
                    Toast.LENGTH_LONG).show();
            // seekBar.setProgress(post.length());
            seekBar.setMax(5);
            gallery.setImageResource(_issueList.get(seekValue));

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            int seekValue = mybar.getProgress();
            // _issueList.set(seekValue, _coverImage);
            Toast.makeText(ReaderScreen.this, "" + seekValue,
                    Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            // Toast.makeText(ReaderScreen.this,"onStopTrackingTouch",
            // Toast.LENGTH_LONG).show();

        }
    });

    //issues_images_WS();
}


// SERVER RESPONSE METHOD
public void serverResponse(String response, int processid) {
    Message msg = new Message();
    msg.arg1 = processid;
    msg.obj = response;
}

public class ImageAdapter extends BaseAdapter {
    /** The parent context */
    private Context myContext;
    ArrayList<Integer> _issueList1 = new ArrayList<Integer>();

    // Put some images to project-folder: /res/drawable/
    // format: jpg, gif, png, bmp, ...
    /*
     * private int[] myImageIds = { R.drawable.ic_launcher,
     * R.drawable.ic_launcher, R.drawable.ic_launcher,
     * R.drawable.ic_launcher };
     */

    /** Simple Constructor saving the 'parent' context. */
    public ImageAdapter(Context c, ArrayList<Integer> list) {
        this.myContext = c;
        this._issueList1 = list;
    }

    private class ViewHolder {

        public ImageView issuesimages;

    }

    // inherited abstract methods - must be implemented
    // Returns count of images, and individual IDs
    public int getCount() {
        return this._issueList1.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    // Returns a new ImageView to be displayed,
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        final ViewHolder viewHolder;

        if (v == null) {
            viewHolder = new ViewHolder();
            LayoutInflater linf = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = linf.inflate(R.layout.imageview, null);
            viewHolder.issuesimages = (ImageView) v
                    .findViewById(R.id.image_view_comic);
            v.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) v.getTag();
        }
        viewHolder.issuesimages.setImageResource(_issueList1.get(position));
 //         String imageUrl = _issueList1.get(position);
 //         viewHolder.issuesimages.setTag(imageUrl);
        return v;

    }
}
}