0

オンタッチ画像ズームとモバイル画面ズームアウトの他のセクションに触れるために、ここから1つの例を試しているので、単にこのリンクを見つけてコードに実装しようとしましたが、次のようなエラーがあります

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

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 に答える