在Android中捏缩放特定形状

时间:2014-06-03 07:06:53

标签: android bitmap imageview shape

我正在custom ImageView,我必须在custom shape内设置像多边形,星形等图像。我已成功在自定义形状中添加图像。但现在我想在图像上执行pinch to zoom。只有图像应该能够变焦,形状应保持原样。我不知道如何继续前进。

以下是我用于在形状内设置图像的代码:

@Override
    protected void onDraw(Canvas canvas) {

        if (!isInitialized) {
            int w = getWidth();
            int h = getHeight();
            position.set(w / 2, h / 2);
            isInitialized = true;
        }

        transform.reset();
        transform.postTranslate(-width / 2.0f, -height / 2.0f);
        transform.postRotate(getDegreesFromRadians(angle));

        if (scale > 1)
            transform.postScale(scale, scale);

        transform.postTranslate(position.getX(), position.getY());

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        mCanvas.drawBitmap(src, transform, null);// bg

        mCanvas.drawBitmap(mask, 0, 0, paint);// object
        paint.setXfermode(null);

        canvas.drawBitmap(result, 0, 0, paint);
    }

enter image description here enter image description here

编辑:
使用上面的代码,有些图像已经能够翻译和缩放。但即将出现的问题是 - 它在画布上显示重复的图画。我不知道为什么它会这样显示。

请向我提供一些有关如何进一步处理的帮助/指导。
任何帮助将非常感谢 感谢。

1 个答案:

答案 0 :(得分:0)

case R.id.zoom:
            Intent zoomIntent = new Intent(con, ImageViewer.class);
            JSONObject jImage;
            try {
                jImage = new JSONObject(jbImage.getString(swiped));
                if(jImage!=null){
                    Log.d("Inventory Cars Details::::::","JImage Lentgh"+jImage.length());
                    String url=jImage.getString("imageURL");
                    if(url!=null){
                        Log.d("Inventory Cars Details::::::","Url Not Null"+jImage.length());
                        if(!url.equalsIgnoreCase("null")){
                            Log.d("Inventory Cars Details::::::","Url string is not Null::"+url);
                            zoomIntent.putExtra("image", jImage.getString("imageURL"));
                        }else{
                            Log.d("Inventory Cars Details::::::","Url string is Null::"+url);
                            zoomIntent.putExtra("image", commonImageURL);
                        }
                    }else{
                        Log.d("Inventory Cars Details::::::","Url is Null::");
                        zoomIntent.putExtra("image", commonImageURL);
                    }
                }else{
                    zoomIntent.putExtra("image", commonImageURL);
                }
                zoomIntent.putExtra("title",
                        jb.getString("year") + " " + jb.getString("c_make")
                        + " " + jb.getString("s_model"));
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            startActivityForResult(zoomIntent,DealershipApplication.REQUEST_CODE);
            break;
        }
    }