如何获得多个缩放图像以适应其缩放尺寸并将其移动到我愿望的屏幕上的任何位置?

时间:2014-03-04 13:03:06

标签: android android-layout

我是android的新手。我正在做一个包含两个垂直线性布局的项目,左线性和右线性。线性包含图像。应该将图像拖动到右线性,它们应该被缩放并移动到屏幕上的任何位置用户希望一个接一个。并且对于缩放,可以使用整个屏幕(同样对于所有图像,因此在缩放第二图像时,它甚至可以在第一图像上出现,并且我应该将组合图像一个在另一个上用于显示) .finally rightlinear应显示所有缩放和拖动的图像。请帮助我。我的主要活动是

            package com.example.example;
            import java.io.File;
            import java.io.FileOutputStream;
            import java.util.Random;
            import android.graphics.Bitmap.CompressFormat;
            import android.util.Log;
            import android.view.DragEvent;
            import android.view.MotionEvent;
            import android.view.View;
            import android.view.View.DragShadowBuilder;
             import android.view.View.OnClickListener;
             import android.view.View.OnDragListener;
           import android.view.View.OnTouchListener;
           import android.widget.Button;
           import android.widget.ImageView;
          import android.widget.LinearLayout;
          import android.widget.RelativeLayout;

 public class MainActivity extends Activity implements  OnTouchListener,OnDragListener{
ImageView iv1,iv2,iv3,iv4,iv5,iv6,iv7,iv8;
Button display,save;
LinearLayout leftlinear;
LinearLayout rightlinear;
private static final String LOGCAT = null;
ImageView image;
AlertDialog option;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv1=(ImageView)findViewById(R.id.imageView1);
    iv2=(ImageView)findViewById(R.id.imageView2);
    iv3=(ImageView)findViewById(R.id.imageView3);
    iv4=(ImageView)findViewById(R.id.imageView4);
    iv5=(ImageView)findViewById(R.id.imageView5);
    iv6=(ImageView)findViewById(R.id.imageView6);
    iv7=(ImageView)findViewById(R.id.imageView7);
    display=(Button)findViewById(R.id.button1);
    save=(Button)findViewById(R.id.button2);
    leftlinear=(LinearLayout)findViewById(R.id.leftlinear);
    rightlinear=(LinearLayout)findViewById(R.id.rightlinear);
     iv1.setDrawingCacheEnabled(true);
     iv2.setDrawingCacheEnabled(true);
     iv3.setDrawingCacheEnabled(true);
     iv4.setDrawingCacheEnabled(true);
     iv5.setDrawingCacheEnabled(true);
     iv6.setDrawingCacheEnabled(true);
     iv7.setDrawingCacheEnabled(true);
    iv1.setOnTouchListener(this);
    iv1.setDrawingCacheEnabled(true);
    iv2.setOnTouchListener(this);
    iv2.setDrawingCacheEnabled(true);
    iv3.setOnTouchListener(this);
    iv3.setDrawingCacheEnabled(true);
    iv4.setOnTouchListener(this);
    iv4.setDrawingCacheEnabled(true);
    iv5.setOnTouchListener(this);
    iv5.setDrawingCacheEnabled(true);
    iv6.setOnTouchListener(this);
    iv6.setDrawingCacheEnabled(true);
    iv7.setOnTouchListener(this);
    iv7.setDrawingCacheEnabled(true);

    leftlinear.setOnDragListener(this);
    rightlinear.setOnDragListener(this);        
    iv1.setDrawingCacheEnabled(true);

    display.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            iv1.setVisibility(0);
            iv2.setVisibility(0);
            iv3.setVisibility(0);
            iv4.setVisibility(0);
            iv5.setVisibility(0);
            iv6.setVisibility(0);
            iv7.setVisibility(0);



        }
    });
    save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            rightlinear.setDrawingCacheEnabled(true);
            rightlinear.buildDrawingCache();
            Bitmap bitmap = rightlinear.getDrawingCache();
            File resultingfile;  
            try {
                File rootFile=new          File(Environment.getExternalStorageDirectory().toString()+"/MYBOARD");
                rootFile.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-"+ n +".png";

                resultingfile=new File(rootFile, fname);

                if (resultingfile.exists ()) resultingfile.delete (); 
                try {
                       FileOutputStream Fout = new FileOutputStream(resultingfile);
                       bitmap.compress(CompressFormat.PNG, 100, Fout);
                       Fout.flush();
                       Fout.close();

                } catch (Exception e) {
                       e.printStackTrace();
                }
          } catch (Exception e) {
        }



        }
    });

}
   public boolean onTouch(View view, MotionEvent motionEvent) { 
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
      view.startDrag(null, shadowBuilder, view, 0);      
      return true;
    }
    else {
        return false;
    }
   }  
public boolean onDrag(View layoutview, DragEvent dragevent) {
      int action = dragevent.getAction();
      switch (action) {
      case DragEvent.ACTION_DRAG_STARTED:
          Log.d(LOGCAT, "Drag event started");
        break;
      case DragEvent.ACTION_DRAG_ENTERED:
          Log.d(LOGCAT, "Drag event entered into "+layoutview.toString());
        break;
      case DragEvent.ACTION_DRAG_EXITED:
          Log.d(LOGCAT, "Drag event exited from "+layoutview.toString());
        break;
      case DragEvent.ACTION_DROP:
        Log.d(LOGCAT, "Dropped");           
        image=new ImageView(this);
        View view = (View) dragevent.getLocalState();
        image.setImageBitmap(view.getDrawingCache());
        LinearLayout container = (LinearLayout) layoutview;         
        container.addView(image);
        image.setVisibility(View.VISIBLE);
        image.setAdjustViewBounds(true);
        LinearLayout.LayoutParams params = new                 LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        image.setLayoutParams(params);
        image.setScaleType(ImageView.ScaleType.MATRIX );

        image.setOnTouchListener(new myTouchListener());            


        break;
      case DragEvent.ACTION_DRAG_ENDED:
              Log.d(LOGCAT, "Drag ended");
          break;
      default:
        break;
      }
      return true;

}

}

    and myTouchListener class is(I copied it from the net and made necessary changes) 

         package com.example.example;
         import android.graphics.Matrix;
         import android.graphics.PointF;
         import android.util.FloatMath;
          import android.util.Log;
         import android.view.MotionEvent;
        import android.view.View;
        import android.view.View.OnTouchListener;
          import android.view.ViewGroup.LayoutParams;
           import android.widget.ImageView;
         import android.widget.LinearLayout;

        public class myTouchListener implements OnTouchListener {
      private static final String TAG = "Touch" ;

    // These matrices will be used to move and zoom image
    //These matrices will be used to move and zoom image  
    Matrix matrix = new Matrix();  
      Matrix savedMatrix = new Matrix();  

// 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;  

// Remember some things for zooming  
PointF start = new PointF();  
PointF mid = new PointF();  
float oldDist = 1f;  
@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    ImageView view = (ImageView) v;     
      // Dump touch event to log  
      dumpEvent(event);  

      // Handle touch events here...  
      switch (event.getAction() & MotionEvent.ACTION_MASK) { 

      case MotionEvent.ACTION_DOWN:  
       savedMatrix.set(matrix);  
       start.set(event.getX(), event.getY());  
       mode = DRAG;  
       break;  
      case MotionEvent.ACTION_POINTER_DOWN:  
       oldDist = spacing(event);  
       if (oldDist > 10f) {  
        savedMatrix.set(matrix);  
        midPoint(mid, event);  
        mode = ZOOM;  
       }  
       break;  
      case MotionEvent.ACTION_UP:  
      case MotionEvent.ACTION_POINTER_UP:  
       mode = NONE;  
       break;  
      case MotionEvent.ACTION_MOVE:  
       if (mode == DRAG) {  
        // ...      
        matrix.set(savedMatrix);  
        matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);  


       } else if (mode == ZOOM) {  
        float newDist = spacing(event);  
        if (newDist > 10f) {  
         matrix.set(savedMatrix);  
         float scale = newDist / oldDist;  
         matrix.postScale(scale, scale, mid.x, mid.y); 
        }  
       }  
       break;  
      }  
      view.setImageMatrix(matrix);
      return true; 
      // indicate event was handled  
     }  
     /** Show an event in the LogCat view, for debugging */  
     private void dumpEvent(MotionEvent event) {  
      String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",  
        "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };  
      StringBuilder sb = new StringBuilder();  
      int action = event.getAction();  
      int actionCode = action & MotionEvent.ACTION_MASK;  
      sb.append("event ACTION_").append(names[actionCode]);  
      if (actionCode == MotionEvent.ACTION_POINTER_DOWN  
        || actionCode == MotionEvent.ACTION_POINTER_UP) {  
       sb.append("(pid ").append(  
         action >> MotionEvent.ACTION_POINTER_ID_SHIFT);  
       sb.append(")");  
      }  

      sb.append("[");  
      for (int i = 0; i < event.getPointerCount(); i++) {  
       sb.append("#").append(i);  
       sb.append("(pid ").append(event.getPointerId(i));  
       sb.append(")=").append((int) event.getX(i));  
       sb.append(",").append((int) event.getY(i));  
       if (i + 1 < event.getPointerCount())  
        sb.append(";");  
      }  
      sb.append("]");  
     }  

     /** Determine the space between the first two fingers */  
     private float spacing(MotionEvent event) {  
      float x = event.getX(0) - event.getX(1);  
      float y = event.getY(0) - event.getY(1);  
      return FloatMath.sqrt(x * x + y * y);  
     }  

     /** Calculate the mid point of the first two fingers */  
     private void midPoint(PointF point, MotionEvent event) {  
      float x = event.getX(0) + event.getX(1);  
      float y = event.getY(0) + event.getY(1);  
      point.set(x / 2, y / 2);  
     }  
    }  

我的xml文件是

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="14dp"
    android:text="Display" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="16dp"
    android:text="Save" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingTop="50dp"
     >

    <LinearLayout
        android:id="@+id/leftlinear"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/new1"

            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/new2"

            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/new3"

            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/new4"

            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image5"

            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image6"

            android:visibility="invisible" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"

            android:visibility="invisible" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/rightlinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center">

    </LinearLayout>
</LinearLayout>

</RelativeLayout>

0 个答案:

没有答案