0

我正在尝试从这里的一个示例进行ontouch图像缩放并触摸移动屏幕缩小的其他部分,所以我找到了这个链接并尝试在代码中实现,但是有一些错误,比如

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

请帮帮我...

4

3 回答 3

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

或者如果你没有在你的项目中添加一个类 ImageZoomView 然后请添加它,如示例Here

于 2012-04-06T05:36:46.843 回答
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" />
于 2012-04-06T05:37:13.570 回答
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);
    }
}
}
于 2012-04-06T05:58:37.083 回答