2

有人可以帮我在android的布局内实现多个图像的捏缩放。我已经在这段代码中完成了边界限制的拖动。现在我想缩放图像。我在这里分享我的代码,用于拖动具有边界限制的多个图像

TouchActivity extends Activity{
 /** Called when the activity is first created. */
    ImageView robot=null,bird=null;
    AbsoluteLayout aLayout,aLayout1;
    int status=0,status1=0;
    // We can be in one of these 3 states
    static final int           NONE                    = 0;
    static final int           DRAG                    = 1;
    static final int           ZOOM                    = 2;
    int                        mode                    = NONE;

    int                        displayHeight, displayWidth;
    Matrix                     matrix                  = new Matrix();
    Matrix                     savedMatrix             = new Matrix();
    Matrix                     matrixForView1          = new Matrix();
    Matrix                     matrixForView2          = new Matrix();
    PointF                     start                   = new PointF();
    PointF                     mid                     = new PointF();
    float                      oldDist                 = 1f;

    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        aLayout= (AbsoluteLayout)findViewById(R.id.absLayout);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics( metrics );
        displayHeight = metrics.heightPixels;
        displayWidth = metrics.widthPixels;

        robot=(ImageView)findViewById(R.id.robot);
        bird=(ImageView)findViewById(R.id.bird);



        bird.setOnTouchListener(new OnTouchListener() {


            @Override
            public boolean onTouch(View v, MotionEvent event) {


                status1=1;
                Log.i("Touched BIRD ImageStatus1",""+status1);
                //robot.setBackgroundColor(Color.WHITE);


                return false;
            }
        });
        robot.setOnTouchListener(new OnTouchListener() {


            @Override
            public boolean onTouch(View v, MotionEvent event) {


                status=1;
                Log.i("Touched ROBOT ImageStatus",""+status);
                //robot.setBackgroundColor(Color.WHITE);


                return false;
            }
        });

        aLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                Log.i("touch",""+event);
                LayoutParams lp;
                if((event.getX()>0&&event.getX()<320)&&(event.getY()>0 && event.getY()<480))
                {
                if(status==1) // any event from down and move
                {
                    lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-robot.getWidth()/2,(int)event.getY()-robot.getHeight()/2);
                    robot.setLayoutParams(lp);



                }
                if(status1==1) // any event from down and move
                {
                    Log.e("event.getX()", String.valueOf(event.getX()));
                    Log.e("event.getY()", String.valueOf(event.getY()));

                    lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-bird.getWidth()/2,(int)event.getY()-bird.getHeight()/2);
                    bird.setLayoutParams(lp);


                }
                }
                else
                {
                    Log.e("image outside boundary","image outside boundary");
                }

                if(event.getAction()==MotionEvent.ACTION_UP){


                    if(status==1)
                    {
                    status=0;

                    robot.setBackgroundColor(Color.TRANSPARENT);
                    }
                    if(status1==1)
                    {    status1=0;

                    bird.setBackgroundColor(Color.TRANSPARENT);


                    }
                                }
                return true;

            }

        });



    }

}

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

         <AbsoluteLayout
        android:id="@+id/absLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/robot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:scaleType="matrix"
           />


        <ImageView
            android:id="@+id/bird"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/white_bird"
           android:scaleType="matrix"

            android:layout_x="200dp"
           />

    </AbsoluteLayout>




</LinearLayout>

可以在这段代码中做到这一点吗?矩阵方法对我有用吗?

提前致谢!

4

1 回答 1

1

您可以WebView为此使用:

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file:/"+ base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadData(html, "text/html","utf-8");
于 2012-09-28T07:32:31.513 回答