Failed to display image image and other content on the same interface in Android?

I am new to android, someone can help me, I am following this ZOOM IMAGEVIEW CLASS tutorial my problem is that I want to click on the image when I click on it, proceeding to the next step, which will show the previous image zoom operation at the top and the rest of the content is in the rest, but right now I have not been able to load my XML file here using this tutorial. It just scales the image.

The code is here:

 protected void onCreate(Bundle savedInstanceState)
    {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample); // i want to show this layout how can i show zoom image in this layout.
    Intent intent = getIntent();
    String stringData = intent.getStringExtra("imageName");//this is image file name
    Log.i("stringData",""+stringData);

    String PACKAGE_NAME = getApplicationContext().getPackageName();
    int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+stringData , null, null);
    System.out.println("IMG ID :: "+imgId);
    System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
     TouchImageView touch = new TouchImageView(this);
    touch.setImageBitmap(bitmap);
    touch.setMaxZoom(4f); //change the max level of zoom, default is 3f

    //ImageView image = (ImageView)findViewById(R.id.img1);
    //image.setImageBitmap(bitmap);
    setContentView(touch);
}

Here is the Xml:

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

        <ImageView
            android:id="@+id/img1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_ask_fatwa_one" />

    <LinearLayout 
        android:id="@+id/layout_buttons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <ImageView 
        android:id="@+id/btn_email_fatwa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/email"
        android:layout_weight="1"/>    
     <ImageView 
        android:id="@+id/btn_share_fatwa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/share_fatwa"
        android:layout_weight="1"/> 
        <ImageView 
        android:id="@+id/btn_save_fatwa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/save_fatwa"
        android:layout_weight="1"/>    

    </LinearLayout>    

</LinearLayout>
+1
source share
1 answer

setContentView(touch);, setContentView(R.layout.sample);
, , TouchImageView sample.xml Layout, .

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

    <ImageView
        android:id="@+id/img1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_ask_fatwa_one" />

   <com.example.zoomimagesample.TouchImageView
        android:id="@+id/YOUR_DESIRED_ID"
        android:layout_width="fill_parent"
        android:layout_height="OUR_DESIRED_HEIGHT"
         />

<LinearLayout 
    android:id="@+id/layout_buttons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
<ImageView 
    android:id="@+id/btn_email_fatwa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/email"
    android:layout_weight="1"/>    
 <ImageView 
    android:id="@+id/btn_share_fatwa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/share_fatwa"
    android:layout_weight="1"/> 
    <ImageView 
    android:id="@+id/btn_save_fatwa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/save_fatwa"
    android:layout_weight="1"/>    

</LinearLayout>    


onCreate(...) :

protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.sample); // i want to show this layout how can i show zoom image in this layout.
Intent intent = getIntent();
String stringData = intent.getStringExtra("imageName");//this is image file name
Log.i("stringData",""+stringData);

String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+stringData , null, null);
System.out.println("IMG ID :: "+imgId);
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
TouchImageView touch = (TouchImageView)findViewById(R.id.ID_THAT_YOU_ASSIGNED_TO_THE_TOUCH_IMAGE)VIEW);
touch.setImageBitmap(bitmap);
touch.setMaxZoom(4f); //change the max level of zoom, default is 3f

//ImageView image = (ImageView)findViewById(R.id.img1);
//image.setImageBitmap(bitmap);
//setContentView(touch);
}


EDIT:
, XML:

:

public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   super.setClickable(true);
   this.context = context;
   mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
   matrix.setTranslate(1f, 1f);
   m = new float[9];
   setImageMatrix(matrix);
   setScaleType(ScaleType.MATRIX);

   setOnTouchListener(new OnTouchListener() {

       @Override
       public boolean onTouch(View v, MotionEvent event) {
           mScaleDetector.onTouchEvent(event);

           matrix.getValues(m);
           float x = m[Matrix.MTRANS_X];
           float y = m[Matrix.MTRANS_Y];
           PointF curr = new PointF(event.getX(), event.getY());

           switch (event.getAction()) {
               case MotionEvent.ACTION_DOWN:
                   last.set(event.getX(), event.getY());
                   start.set(last);
                   mode = DRAG;
                   break;
               case MotionEvent.ACTION_POINTER_DOWN:
                   last.set(event.getX(), event.getY());
                   start.set(last);
                   mode = ZOOM;
                   break;
               case MotionEvent.ACTION_MOVE:
                   if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) {
                       Log.d("******", "ZOOM OR DRAG");
                       float deltaX = curr.x - last.x;
                       float deltaY = curr.y - last.y;
                       float scaleWidth = Math.round(origWidth * saveScale);
                       float scaleHeight = Math.round(origHeight * saveScale);
                       if (scaleWidth < width) {
                           deltaX = 0;
                           if (y + deltaY > 0)
                               deltaY = -y;
                           else if (y + deltaY < -bottom)
                               deltaY = -(y + bottom);
                       } else if (scaleHeight < height) {
                           deltaY = 0;
                           if (x + deltaX > 0)
                               deltaX = -x;
                           else if (x + deltaX < -right)
                               deltaX = -(x + right);
                       } else {
                           if (x + deltaX > 0)
                               deltaX = -x;
                           else if (x + deltaX < -right)
                               deltaX = -(x + right);

                           if (y + deltaY > 0)
                               deltaY = -y;
                           else if (y + deltaY < -bottom)
                               deltaY = -(y + bottom);
                       }
                       matrix.postTranslate(deltaX, deltaY);
                       last.set(curr.x, curr.y);
                   }else if(mode == DRAG && saveScale == minScale) {
                       Log.d("******", "DRAG");
                   }
                   break;

               case MotionEvent.ACTION_UP:
                   mode = NONE;
                   int xDiff = (int) Math.abs(curr.x - start.x);
                   int yDiff = (int) Math.abs(curr.y - start.y);
                   if (xDiff < CLICK && yDiff < CLICK)
                       performClick();
                   break;

               case MotionEvent.ACTION_POINTER_UP:
                   mode = NONE;
                   break;
           }
           setImageMatrix(matrix);
           invalidate();
           return true; // indicate event was handled
       }

   });
  }

  public TouchImageView(Context context, AttributeSet attrs) {
   super(context, attrs);
   super.setClickable(true);
   this.context = context;
   mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
   matrix.setTranslate(1f, 1f);
   m = new float[9];
   setImageMatrix(matrix);
   setScaleType(ScaleType.MATRIX);

   setOnTouchListener(new OnTouchListener() {

       @Override
       public boolean onTouch(View v, MotionEvent event) {
           mScaleDetector.onTouchEvent(event);

           matrix.getValues(m);
           float x = m[Matrix.MTRANS_X];
           float y = m[Matrix.MTRANS_Y];
           PointF curr = new PointF(event.getX(), event.getY());

           switch (event.getAction()) {
               case MotionEvent.ACTION_DOWN:
                   last.set(event.getX(), event.getY());
                   start.set(last);
                   mode = DRAG;
                   break;
               case MotionEvent.ACTION_POINTER_DOWN:
                   last.set(event.getX(), event.getY());
                   start.set(last);
                   mode = ZOOM;
                   break;
               case MotionEvent.ACTION_MOVE:
                   if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) {
                       Log.d("******", "ZOOM OR DRAG");
                       float deltaX = curr.x - last.x;
                       float deltaY = curr.y - last.y;
                       float scaleWidth = Math.round(origWidth * saveScale);
                       float scaleHeight = Math.round(origHeight * saveScale);
                       if (scaleWidth < width) {
                           deltaX = 0;
                           if (y + deltaY > 0)
                               deltaY = -y;
                           else if (y + deltaY < -bottom)
                               deltaY = -(y + bottom);
                       } else if (scaleHeight < height) {
                           deltaY = 0;
                           if (x + deltaX > 0)
                               deltaX = -x;
                           else if (x + deltaX < -right)
                               deltaX = -(x + right);
                       } else {
                           if (x + deltaX > 0)
                               deltaX = -x;
                           else if (x + deltaX < -right)
                               deltaX = -(x + right);

                           if (y + deltaY > 0)
                               deltaY = -y;
                           else if (y + deltaY < -bottom)
                               deltaY = -(y + bottom);
                       }
                       matrix.postTranslate(deltaX, deltaY);
                       last.set(curr.x, curr.y);
                   }else if(mode == DRAG && saveScale == minScale) {
                       Log.d("******", "DRAG");
                   }
                   break;

               case MotionEvent.ACTION_UP:
                   mode = NONE;
                   int xDiff = (int) Math.abs(curr.x - start.x);
                   int yDiff = (int) Math.abs(curr.y - start.y);
                   if (xDiff < CLICK && yDiff < CLICK)
                       performClick();
                   break;

               case MotionEvent.ACTION_POINTER_UP:
                   mode = NONE;
                   break;
           }
           setImageMatrix(matrix);
           invalidate();
           return true; // indicate event was handled
       }

   });
 }

, .

+2

Source: https://habr.com/ru/post/1524144/


All Articles