2012-04-06 64 views
0

我试图从Here一个例子为ontouch的图像变焦和触控上的其他部分手机屏幕缩小如此简单,我有发现这个链接,并尝试在代码中实现,但也有一些错误,如生效ontouch Android的放大图像

04-06 10:58:53.618: ERROR/AndroidRuntime(493): FATAL EXCEPTION: main 
04-06 10:58:53.618: ERROR/AndroidRuntime(493): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Zoom_imageview/com.Zoom_imageview.Zoom_imageviewActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.sonyericsson.zoom.ImageZoomView 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.os.Looper.loop(Looper.java:123) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at dalvik.system.NativeStart.main(Native Method) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.sonyericsson.zoom.ImageZoomView 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.view.LayoutInflater.inflate(LayoutInflater.java:385) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.Activity.setContentView(Activity.java:1647) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at com.Zoom_imageview.Zoom_imageviewActivity.onCreate(Zoom_imageviewActivity.java:38) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-06 10:58:53.618: ERROR/AndroidRuntime(493):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 

代码:

package com.Zoom_imageview; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

import com.Zoom_imageview.zoom.ImageZoomView; 
import com.Zoom_imageview.zoom.SimpleZoomListener; 
import com.Zoom_imageview.zoom.ZoomState; 
import com.Zoom_imageview.zoom.SimpleZoomListener.ControlType; 

public class Zoom_imageviewActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private static final int MENU_ID_ZOOM = 0; 

    /** Constant used as menu item id for setting pan control type */ 
    private static final int MENU_ID_PAN = 1; 

    /** Constant used as menu item id for resetting zoom state */ 
    private static final int MENU_ID_RESET = 2; 

    /** Image zoom view */ 
    private ImageZoomView mZoomView; 

    /** Zoom state */ 
    private ZoomState mZoomState; 

    /** Decoded bitmap image */ 
    private Bitmap mBitmap; 
    private SimpleZoomListener mZoomListener; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     try 
     { 
     mZoomState = new ZoomState(); 

     mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 

     mZoomListener = new SimpleZoomListener(); 
     mZoomListener.setZoomState(mZoomState); 

     mZoomView = (ImageZoomView)findViewById(R.id.zoomview); 
     mZoomView.setZoomState(mZoomState); 
     mZoomView.setImage(mBitmap); 
     mZoomView.setOnTouchListener(mZoomListener); 

     resetZoomState(); 
     }catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

     mBitmap.recycle(); 
     mZoomView.setOnTouchListener(null); 
     mZoomState.deleteObservers(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     menu.add(Menu.NONE, MENU_ID_ZOOM, 0, R.string.menu_zoom); 
     menu.add(Menu.NONE, MENU_ID_PAN, 1, R.string.menu_pan); 
     menu.add(Menu.NONE, MENU_ID_RESET, 2, R.string.menu_reset); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case MENU_ID_ZOOM: 
       mZoomListener.setControlType(ControlType.ZOOM); 
       break; 

      case MENU_ID_PAN: 
       mZoomListener.setControlType(ControlType.PAN); 
       break; 

      case MENU_ID_RESET: 
       resetZoomState(); 
       break; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * Reset zoom state and notify observers 
    */ 
    private void resetZoomState() { 
     mZoomState.setPanX(0.5f); 
     mZoomState.setPanY(0.5f); 
     mZoomState.setZoom(1f); 
     mZoomState.notifyObservers(); 
    } 




} 

XML ::

<?xml version="1.0" encoding="utf-8"?> 
<com.sonyericsson.zoom.ImageZoomView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/zoomview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 

请帮我...

+0

'com.sonyericsson.zoom.ImageZoomView'这是你的包名如果没有然后将其更改为'com.Zoom_imageview.zoom。 ImageZoomView;' – 2012-04-06 05:36:14

+0

没有它不是....... – 2012-04-06 05:37:03

+0

然后'com.sonyericsson.zoom.ImageZoomView'到'com.Zoom_imageview.zoom.ImageZoomView;'在XML中看到我的回答 – 2012-04-06 05:38:18

回答

2
<?xml version="1.0" encoding="utf-8"?> 
<com.sonyericsson.zoom.ImageZoomView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/zoomview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 

将其更改为

<?xml version="1.0" encoding="utf-8"?> 
    <com.Zoom_imageview.zoom.ImageZoomView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/zoomview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     /> 

或者如果u有没有在你的项目中添加一个类ImageZoomView然后PLZ其添加为例子Here

+0

你能告诉我怎样才能放大在单点触摸 – 2012-04-06 06:07:31

+0

@ user1129443形象:没有对不起朋友,我知道do't – 2012-04-06 06:15:47

+0

@ user1129443好了,我支持放大和图像视图用了两指,如果你有兴趣,http://code.google.com/p/mashpotato/source/browse/trunk/src/com /mashpotato/widget/BetterImageView.java – idiottiger 2012-04-06 06:20:15

0

对此用xml改变

<com.Zoom_imageview.Zoom_imageviewActivity 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/zoomview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
0

试试吧 -

package com.activity; 

import com.layout.ZoomImageViewer; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.os.Bundle; 

public class FullScreenImageViewer extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ZoomImageViewer zoom = new ZoomImageViewer(this); 
     zoom.setBackgroudImage((Bitmap)getIntent().getParcelableExtra("image")); 
     setContentView(zoom); 
    } 
} 

和第二类

package com.layout; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.util.AttributeSet; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.ZoomControls; 

public class ZoomImageViewer extends RelativeLayout { 
private final Context context; 
private int zoomControler   = 100; 
private ViewImage viewImage   = null; 
private ZoomControls zoom   = null; 

private boolean isLowVirtualMemory = false; 

public ZoomImageViewer(Context context) { 
    super(context); 
    this.context = context; 
    setLayout(); 
} 

public ZoomImageViewer(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
    setLayout(); 
} 

public ZoomImageViewer(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    this.context = context; 
    setLayout(); 
} 

private void setLayout() { 
    setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    viewImage = new ViewImage(context); 
    addView(viewImage); 

    RelativeLayout layout = new RelativeLayout(context); 
    LayoutParams params  = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    zoom     = new ZoomControls(context); 

    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    layout.setLayoutParams(params); 

    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.CENTER_HORIZONTAL); 
    zoom.setLayoutParams(params); 
    zoom.setVisibility(View.GONE); 
    zoom.setZoomSpeed(0); 
    layout.addView(zoom); 
    addView(layout); 

    zoom.setOnZoomInClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      viewImage.onZoom(true); 
     } 
    }); 

    zoom.setOnZoomOutClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      viewImage.onZoom(false); 
     } 
    }); 
} 

public void setBackgroudImage(byte[] byteImage) { 
    setBackgroudImage(BitmapFactory.decodeByteArray(byteImage, 0, byteImage.length)); 
} 

public void setBackgroudImage(Bitmap bm) { 
    if(viewImage != null) 
     viewImage.setImage(bm); 
} 

public void setBackgroudImage(int id) { 
    if(viewImage != null) 
     viewImage.setImage(((BitmapDrawable)context.getResources().getDrawable(id)).getBitmap()); 
} 

private final class ViewImage extends ImageView implements OnTouchListener { 
    private final int MAX_SIZE = 1500; 
    private final int MIN_SIZE = 80; 
    private Bitmap bitmap  = null; 
    private LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    private float mx, my, curX, curY; 

    public ViewImage(Context context) { 
     super(context); 
     setLayout(); 
    } 

    public ViewImage(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setLayout(); 
    } 

    public ViewImage(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setLayout(); 
    } 

    private void setLayout() { 
     params.addRule(RelativeLayout.CENTER_IN_PARENT); 
     setLayoutParams(params); 
     setScaleType(ScaleType.CENTER); 
     setFocusable(true); 
     setOnTouchListener(this); 
    } 

    public void setImage(Bitmap bitmap) { 
     if(bitmap == null) { 
      zoom.setVisibility(View.GONE); 
      return; 
     } 
     this.bitmap = bitmap; 
     zoom.setVisibility(View.VISIBLE); 
     zoomControler = bitmap.getWidth(); 
     setImageBitmap(Bitmap.createScaledBitmap(this.bitmap, zoomControler, zoomControler, true)); 
    } 

    public void onZoom(boolean zoomIn) { 
     if((zoomIn) && (zoomControler < MAX_SIZE) && (!isLowVirtualMemory)) 
      zoomControler += 10; 
     else { 
      if(zoomControler > MIN_SIZE) 
       zoomControler -= 10; 
     } 
     try { 
      setImageBitmap(Bitmap.createScaledBitmap(this.bitmap, zoomControler, zoomControler, true)); 
      isLowVirtualMemory = false; 
     }catch(OutOfMemoryError e) {     
      isLowVirtualMemory = true; 
     } 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if(keyCode == KeyEvent.KEYCODE_DPAD_UP) // zoom in 
      onZoom(true); 
     else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) // zoom out 
      onZoom(false); 

     return(true); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) {    
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       mx = event.getX(); 
       my = event.getY(); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       curX = event.getX(); 
       curY = event.getY(); 
       scrollBy((int) (mx - curX), (int) (my - curY)); 
       mx = curX; 
       my = curY; 
       break; 
      case MotionEvent.ACTION_UP: 
       curX = event.getX(); 
       curY = event.getY(); 
       scrollBy((int) (mx - curX), (int) (my - curY)); 
       break; 
     } 
     return(true); 
    } 
} 
}