Imageswitcher 中的 Ontouch 多点触控缩放?

发布于 2024-11-29 18:08:29 字数 6914 浏览 0 评论 0原文

我正在尝试在图像切换器上启用捏缩放。我现在陷入如何在 ontouch 方法中声明 imageview 的困境。我意识到 ImageView i = (Imageview) v 是不正确的,我只是不知道将其切换到什么以在 ontouch 中声明 imageswitcher imageview ... 这是我的java和xml。感谢您的帮助!

public class ImageSwitch1 extends Activity implements
        AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory, OnTouchListener {

    Matrix matrix = new Matrix();
    Matrix eventMatrix = new Matrix();

    final static int NONE = 0;
    final static int DRAG = 1;
    final static int ZOOM = 2;
    int touchState = NONE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.imageswitcher);

        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));


        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(this);




    }


    public void onItemSelected(AdapterView parent, View v, int position, long id) {

        final TextView tv = (TextView)findViewById(R.id.SwitcherText);
        mSwitcher.setImageResource(mImageIds[position]);
        tv.setText(mText[position]);

    }  
    public void onNothingSelected(AdapterView parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setScaleType(ScaleType.MATRIX);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
          i.setOnTouchListener(this);
        return i;
    }
    final static float MIN_DIST = 50;
    static float eventDistance = 0;
    static float centerX =0, centerY = 0;
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView) v;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
        //primary touch event starts: remember touch down location
        touchState = DRAG;
        centerX = event.getX(0);
        centerY = event.getY(0);
        eventMatrix.set(matrix);
        break;
        case MotionEvent.ACTION_POINTER_DOWN:
        //secondary touch event starts: remember distance and center
        eventDistance = calcDistance(event);
        calcMidpoint(centerX, centerY, event);
        if (eventDistance > MIN_DIST) {
        eventMatrix.set(matrix);
        touchState = ZOOM;
        }
        break;
        case MotionEvent.ACTION_MOVE:
            if (touchState == DRAG) {
            //single finger drag, translate accordingly
            matrix.set(eventMatrix);
            matrix.setTranslate(event.getX(0) - centerX,
            event.getY(0) - centerY);
            } else if (touchState == ZOOM) {
            //multi-finger zoom, scale accordingly around center
            float dist = calcDistance(event);
            if (dist > MIN_DIST) {
            matrix.set(eventMatrix);
            float scale = dist / eventDistance;
            matrix.postScale(scale, scale, centerX, centerY);
            }
            }
            // Perform the transformation
            i.setImageMatrix(matrix);
            break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
            touchState = NONE;
            break;
            }
            return true;
            }
            private float calcDistance(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
            }
            private void calcMidpoint(float centerX, float centerY,
            MotionEvent event) {
            centerX = (event.getX(0) + event.getX(1))/2;
            centerY = (event.getY(0) + event.getY(1))/2;
            }    


    private ImageSwitcher mSwitcher;

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mThumbIds[position]);
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            i.setBackgroundResource(R.drawable.picture_frame);


            return i;
        }
            private Context mContext;
    }

XML:

<?xml version="1.0" encoding="utf-8"?>


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Rellayout1"
        android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true">
        <RelativeLayout android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:isScrollContainer="true" android:layout_marginBottom="120dp"> 

            <ImageSwitcher android:layout_alignParentLeft="true" 
                android:layout_alignParentTop="true" 
                android:id="@+id/switcher"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:layout_marginTop="60dp"
                >

            </ImageSwitcher>
            <Gallery android:spacing="16dp" android:layout_height="80dp" android:id="@+id/gallery" android:unselectedAlpha="0.5" android:background="#55000000" android:gravity="center_vertical" android:layout_width="match_parent" android:minHeight="20px"></Gallery>


        </RelativeLayout>
        <ScrollView
            android:id="@+id/ScrollViewSwitcher"
            android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="true" android:isScrollContainer="true" android:layout_alignParentBottom="true" android:layout_height="120dp">
            <TextView android:scrollbarAlwaysDrawVerticalTrack="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/SwitcherText" android:textSize="18dp"></TextView>
        </ScrollView>
    </RelativeLayout>

I am trying to enable pinch-zooming on an imageswitcher. I am stuck right now in how to declare the imageview in the ontouch method. I realize that the ImageView i = (Imageview) v is incorrect, I just dont know what to switch it to to declare the imageswitcher imageview in the ontouch...
Here is my java and xml. Thank you for your help!

public class ImageSwitch1 extends Activity implements
        AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory, OnTouchListener {

    Matrix matrix = new Matrix();
    Matrix eventMatrix = new Matrix();

    final static int NONE = 0;
    final static int DRAG = 1;
    final static int ZOOM = 2;
    int touchState = NONE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.imageswitcher);

        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));


        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(this);




    }


    public void onItemSelected(AdapterView parent, View v, int position, long id) {

        final TextView tv = (TextView)findViewById(R.id.SwitcherText);
        mSwitcher.setImageResource(mImageIds[position]);
        tv.setText(mText[position]);

    }  
    public void onNothingSelected(AdapterView parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setScaleType(ScaleType.MATRIX);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
          i.setOnTouchListener(this);
        return i;
    }
    final static float MIN_DIST = 50;
    static float eventDistance = 0;
    static float centerX =0, centerY = 0;
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView) v;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
        //primary touch event starts: remember touch down location
        touchState = DRAG;
        centerX = event.getX(0);
        centerY = event.getY(0);
        eventMatrix.set(matrix);
        break;
        case MotionEvent.ACTION_POINTER_DOWN:
        //secondary touch event starts: remember distance and center
        eventDistance = calcDistance(event);
        calcMidpoint(centerX, centerY, event);
        if (eventDistance > MIN_DIST) {
        eventMatrix.set(matrix);
        touchState = ZOOM;
        }
        break;
        case MotionEvent.ACTION_MOVE:
            if (touchState == DRAG) {
            //single finger drag, translate accordingly
            matrix.set(eventMatrix);
            matrix.setTranslate(event.getX(0) - centerX,
            event.getY(0) - centerY);
            } else if (touchState == ZOOM) {
            //multi-finger zoom, scale accordingly around center
            float dist = calcDistance(event);
            if (dist > MIN_DIST) {
            matrix.set(eventMatrix);
            float scale = dist / eventDistance;
            matrix.postScale(scale, scale, centerX, centerY);
            }
            }
            // Perform the transformation
            i.setImageMatrix(matrix);
            break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
            touchState = NONE;
            break;
            }
            return true;
            }
            private float calcDistance(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
            }
            private void calcMidpoint(float centerX, float centerY,
            MotionEvent event) {
            centerX = (event.getX(0) + event.getX(1))/2;
            centerY = (event.getY(0) + event.getY(1))/2;
            }    


    private ImageSwitcher mSwitcher;

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mThumbIds[position]);
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            i.setBackgroundResource(R.drawable.picture_frame);


            return i;
        }
            private Context mContext;
    }

XML:

<?xml version="1.0" encoding="utf-8"?>


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Rellayout1"
        android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true">
        <RelativeLayout android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:isScrollContainer="true" android:layout_marginBottom="120dp"> 

            <ImageSwitcher android:layout_alignParentLeft="true" 
                android:layout_alignParentTop="true" 
                android:id="@+id/switcher"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:layout_marginTop="60dp"
                >

            </ImageSwitcher>
            <Gallery android:spacing="16dp" android:layout_height="80dp" android:id="@+id/gallery" android:unselectedAlpha="0.5" android:background="#55000000" android:gravity="center_vertical" android:layout_width="match_parent" android:minHeight="20px"></Gallery>


        </RelativeLayout>
        <ScrollView
            android:id="@+id/ScrollViewSwitcher"
            android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="true" android:isScrollContainer="true" android:layout_alignParentBottom="true" android:layout_height="120dp">
            <TextView android:scrollbarAlwaysDrawVerticalTrack="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/SwitcherText" android:textSize="18dp"></TextView>
        </ScrollView>
    </RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2024-12-06 18:08:29

我通过更改这一行解决了这个问题:

ImageView i = (ImageView) v;

By

ImageView i = mSwitcher;

但不能使用矩阵,imageSwitcher 不使用矩阵,或者我不知道它是如何完成的。

我希望能帮助你。

I solved this problem changing this line:

ImageView i = (ImageView) v;

By

ImageView i = mSwitcher;

But can't use matrix, imageSwitcher don't use matrix or i do not know how it's done.

I hope help you with this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文