如何在Android中的以下代码中实现Pinch Zoom

时间:2013-01-12 10:49:03

标签: java android uiimageview pinchzoom

  

可能重复:
  Android Pinch and Zoom Image in Activity

我必须在以下android代码中实现捏缩放功能。如果可能请提供代码。 请注意我在哪里必须在此文件中实现该特定代码。

enter code here
 public class ImagedisplaytestActivity extends Activity {
        private ImageView leftArrowImageView;
        private ImageView rightArrowImageView;
        private Gallery gallery;
        public int selectedImagePosition = 0;
        private GalleryImageAdapter galImageAdapter;
        private String bitmapImg = "";
        Bitmap bitmap = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            setupUI();
        }

 private void setupUI() {
                Intent i = getIntent();
                Bundle extras=i.getExtras();
                bitmapImg = extras.getString("BitmapImage");
                selectedImagePosition = extras.getInt("Pos");

                leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
                rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);
                gallery = (Gallery) findViewById(R.id.gallery);
                leftArrowImageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (selectedImagePosition > 0) {
                            --selectedImagePosition;
                        }
                        gallery.setSelection(selectedImagePosition, false);
                    }
                });

                rightArrowImageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (selectedImagePosition < DetailView.bitmapURL.size() - 1) {
                            ++selectedImagePosition;
                        }
                        gallery.setSelection(selectedImagePosition, false);
                    }
                });

                gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                        selectedImagePosition = pos;
                        if (selectedImagePosition > 0 && selectedImagePosition < DetailView.bitmapURL.size() - 1) {

                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
                        } else if (selectedImagePosition == 0) {
                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                        } else if (selectedImagePosition == DetailView.bitmapURL.size() - 1) {
                            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
                        }

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                    }
                });

                galImageAdapter = new GalleryImageAdapter(this, DetailView.bitmapURL);
                gallery.setAdapter(galImageAdapter);
                if (DetailView.bitmapURL.size() > 0) {
                    gallery.setSelection(selectedImagePosition, false);
                }
                if (DetailView.bitmapURL.size() == 1) {
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
                }

            }

            public class GalleryImageAdapter extends BaseAdapter {
                private Activity context;
                private  ImageView imageView;
                private List<String>  plotsImages;
                private ViewHolder holder;
                public GalleryImageAdapter(Activity context, List<String> plotsImages) {
                    this.context = context;
                    this.plotsImages = plotsImages;
                }

                @Override
                public int getCount() {
                    return plotsImages.size();
                }

                @Override
                public Object getItem(int position) {
                    return null;
                }

                @Override
                public long getItemId(int position) {
                    return 0;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    if (convertView == null) {
                        holder = new ViewHolder();
                        imageView = new ImageView(this.context);
                        imageView.setPadding(3, 3, 3, 3);
                        convertView = imageView;
                        holder.imageView = imageView;
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }
                    holder.imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                    holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);

                    try {
                        bitmap = DownloadImage(plotsImages.get(position));
                        holder.imageView.setImageBitmap(bitmap);
                        bitmap = null;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                            return imageView;
                }

                private class ViewHolder {
                    ImageView imageView;
                }


                 private Bitmap DownloadImage(String URL){
                        Bitmap bitmap = null;
                        try {
                            InputStream in = OpenHttpConnection(URL);
                            bitmap = BitmapFactory.decodeStream(in);
                            in.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        return bitmap;
                    }

                    private InputStream OpenHttpConnection(String urlString) throws IOException{
                        InputStream in = null;
                        int response = -1;
                        URL url = new URL(urlString);
                        URLConnection conn = url.openConnection();

                        if (!(conn instanceof HttpURLConnection)) {
                            throw new IOException("Not an HTTP connection");
                        }
                        try{
                            HttpURLConnection httpConn = (HttpURLConnection) conn;
                            httpConn.setAllowUserInteraction(false);
                            httpConn.setInstanceFollowRedirects(true);
                            httpConn.setRequestMethod("GET");
                            httpConn.connect();
                            response = httpConn.getResponseCode();
                            if (response == HttpURLConnection.HTTP_OK) {
                                in = httpConn.getInputStream();
                            }
                        }
                        catch (Exception ex){
                        throw new IOException("Error connecting");
                        }
                        return in;
                    }

            }

        }

任何帮助都会得到满足。谢谢。

0 个答案:

没有答案