简体   繁体   中英

How to make Imageview bigger than screen size in android?

I'm beginner to android, I want to know how can I upload an image bigger than screen size with the ability to drag it? What layout should I use and how can dragging be done? Just name me what to use and what should I read.

Is this what you are looking for?

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="14dp"
   android:gravity="center"
   tools:context=".MainActivity">
   <ImageView
      android:id="@+id/iv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:src="@drawable/image" />
</LinearLayout>

Java:

import androidx.appcompat.app.AppCompatActivity;
import android.os.*;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
   private ScaleGestureDetector scaleGestureDetector;
   private float mScaleFactor = 1.0f;
   private ImageView imageView;  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      imageView=findViewById(R.id.iv);
      scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
   }
   @Override
   public boolean onTouchEvent(MotionEvent motionEvent) {
      scaleGestureDetector.onTouchEvent(motionEvent);
      return true;
   }
   private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
      @Override
      public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
         mScaleFactor *= scaleGestureDetector.getScaleFactor();
         mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
         imageView.setScaleX(mScaleFactor);
         imageView.setScaleY(mScaleFactor);
         return true;
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM