2015-11-19 2 views
1

Я пытаюсь реализовать щепотку для увеличения, в моем проекте я использовал несколько видов деятельности, которые их соединяют. Первоначально, когда у меня был полный проект в одном действии, он выполнял масштабирование, однако это не относится к нескольким действиям.PinchToZoom не работает с несколькими действиями в Android

КОД: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.kmay.calculatephase"> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:debuggable="true" 
    android:theme="@style/AppTheme" > 
    <activity android:name=".MainActivity" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".LoadImage" > 
    </activity> 
    <activity android:name=".Phase" > 
    </activity> 
    <activity android:name=".ColorGray" > 
    </activity> 
    <activity android:name=".Information" > 
    </activity> 
</application> 

КОД: MainActivity.java

package com.example.stiwari.myappli; 

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Color; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.InputStream; 

public class MainActivity extends AppCompatActivity { 
    Button btn1;Bitmap imgb;Bitmap operation; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btn1 = (Button) findViewById(R.id.btn1); 
     Bitmap bmp; 
     btn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent rawIntent = new Intent(Intent.ACTION_PICK); 
       File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
       String pictureDirectoryPath = pictureDirectory.getPath(); 
       Uri data = Uri.parse(pictureDirectoryPath); 
       rawIntent.setDataAndType(data, "image/*"); 
       startActivityForResult(rawIntent, 10); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode==RESULT_OK){ 
      if(requestCode==10){ 
       final float red = (float) 0.299; 
       final float green = (float) 0.587; 
       final float blue = (float) 0.114; 
       final Uri imagef = data.getData(); 
       InputStream streamI; 
       try { 
        streamI = getContentResolver().openInputStream(imagef); 
        //Create bitmap from selected image 
        imgb = BitmapFactory.decodeStream(streamI); 
        //Define rows and columns of selected image 
        int rows = imgb.getHeight();int cols = imgb.getWidth(); 
        operation = Bitmap.createBitmap(cols, rows, imgb.getConfig()); 
        //Convert original image to Gray Image 
        for (int i=0;i<cols;i++){ 
         for(int j=0;j<rows;j++){ 
          int p = imgb.getPixel(i,j); 
          int r = Color.red(p); 
          int g = Color.green(p); 
          int b = Color.blue(p); 
          r = (int) (red*r); 
          g = (int) (green*g); 
          b = (int) (blue*b); 
          int gray = (int) (r*0.299+g*0.587+b*0.114); 
          operation.setPixel(i, j, Color.argb(Color.alpha(p), gray, gray, gray)); 
         } 
        } 
       } catch (FileNotFoundException e) { 
        e.printStackTrace();} 
      } 
      //Use Intent property to send data to Main2Activity.class 
      Intent i = new Intent(this,Main2Activity.class); 
      ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
      operation.compress(Bitmap.CompressFormat.PNG, 50, bs); 
      i.putExtra("byteArray", bs.toByteArray()); 
      startActivity(i); 
     } 
    } 
} 

КОД: activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Next Page" 
    android:id="@+id/btn1" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

КОД: Main2Activity.java

package com.example.stiwari.myappli; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.MotionEvent; 
import android.view.ScaleGestureDetector; 
import android.view.View; 
import android.widget.ImageView; 
public class Main2Activity extends AppCompatActivity { 
    Bitmap imgb; 
    ImageView imageView1;float scaleFactor;View view; 
    ScaleGestureDetector detector; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 
     imageView1 = (ImageView) findViewById(R.id.imageView1); 
     detector = new ScaleGestureDetector(this, new ScaleListener()); 


    if (getIntent().hasExtra("byteArray")) { 
     //ImageView previewThumbnail = new ImageView(this); 
     imgb = BitmapFactory.decodeByteArray(
       getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length); 
     //previewThumbnail.setImageBitmap(imgb); 
    }imageView1.setImageBitmap(imgb); 

    imageView1.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Main2Activity.this.view = v; 
      detector.onTouchEvent(event); 
      return false; 
     } 
    }); 
} 

КОД: activity_main2.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.example.stiwari.myappli.Main2Activity"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView1" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

ответ

1

Используйте ниже код для Pinch Zoom.

ScaleGestureDetector detector = new ScaleGestureDetector(this, new ScaleListener()); 


private class ScaleListener extends 
     ScaleGestureDetector.SimpleOnScaleGestureListener { 
    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     scaleFactor *= detector.getScaleFactor(); 
     scaleFactor = (scaleFactor < 1 ? 1 : scaleFactor); 
     scaleFactor = ((float) ((int) (scaleFactor * 100)))/100; 
     view.setScaleX(scaleFactor); 
     view.setScaleY(scaleFactor); 
     return true; 
    } 
} 

Кроме того, настроить touchListener чтобы просматривать на OnCreate (тот, который вы хотите, чтобы увеличить). пример

(YOUR_VIEW).setClickable(true); 

(YOUR_VIEW).setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

        MainActivity.this.view = v; 
        detector.onTouchEvent(event); 

       return false; 

     }); 

Надеется, что это поможет :)

UPDATE 1:

на основе вашего состояния code.change как этот

imageView1.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_UP) { 
       } else { 
        Main2Activity.this.view = v; 
      detector.onTouchEvent(event); 
       } 
      return false; 
     } 
    }); 
+0

что посмотреть в view.setScaleX (масштаб); Где я должен объявить об этом? –

+0

Также, что является MainActivity во втором блоке –

+0

Thats our mainActivity ... объявить представление общедоступным (View view;) ... defenition производится на сенсорном listner .. (MainActivity.this.view = v;) .. Got Это? –

Смежные вопросы