使用TouchImageView.java类查找TouchImageView的中心点

时间:2015-07-13 06:10:20

标签: java android

我有两个TouchImageView。我想要实现的是,当我从左向右滑动第一个TouchImageView时,第二个TouchImageView会自动从右向左滑动。我正在使用我在GitHub创建的TouchImageView.java类。以下是我的代码。

activity_main.xml中

    <LinearLayout 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:gravity="center"
    android:orientation="horizontal"
    android:weightSum="2"
    tools:context="com.PinchZoom.pinchzoomexampletwo.MainActivity" >

    <com.PinchZoom.pinchzoomexampletwo.TouchImageView
        android:id="@+id/img_to_be_zoomed"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:src="@drawable/fb" >
    </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

    <com.PinchZoom.pinchzoomexampletwo.TouchImageView
        android:id="@+id/img_to_be_zoomed_mirror"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:src="@drawable/fb" >
    </com.PinchZoom.pinchzoomexampletwo.TouchImageView>

</LinearLayout>

MainActivity.java

package com.PinchZoom.pinchzoomexampletwo;

import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import com.PinchZoom.pinchzoomexampletwo.TouchImageView.OnTouchImageViewListener;

public class MainActivity extends ActionBarActivity {
    TouchImageView img_to_be_zoomed;
    TouchImageView img_to_be_zoomed_mirror;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img_to_be_zoomed = (TouchImageView) findViewById(R.id.img_to_be_zoomed);
        img_to_be_zoomed
                .setOnTouchImageViewListener(new OnTouchImageViewListener() {

                    @Override
                    public void onMove() {

                        img_to_be_zoomed_mirror.setZoom(img_to_be_zoomed);

                        Log.e("X Scroll Point: ",
                                "" + img_to_be_zoomed.getScrollPosition());
                    }
                });

        img_to_be_zoomed_mirror = (TouchImageView) findViewById(R.id.img_to_be_zoomed_mirror);
        img_to_be_zoomed_mirror
                .setOnTouchImageViewListener(new OnTouchImageViewListener() {

                    @Override
                    public void onMove() {
                        // TODO Auto-generated method stub
                        img_to_be_zoomed.setZoom(img_to_be_zoomed_mirror);
                        PointF s = new PointF();
                        PointF q = new PointF();
                        s = img_to_be_zoomed.getScrollPosition();
                        q = img_to_be_zoomed_mirror.getScrollPosition();
                        img_to_be_zoomed.setScrollPosition(s.x
                                - (q.x - img_to_be_zoomed.getX()), s.y);
                        Log.e("img_1", s.x + "/" + s.y);
                        Log.e("img_2", "" + q.x + "/" + q.y);
                    }
                });
    }
}

TouchImageView.java

TouchImageView.java Find Here

修改 我使用MainActivity.java中的以下代码解决了问题。

PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed.getScrollPosition();
img_to_be_zoomed_mirror.setScrollPosition(1 - pointF_img1.x, pointF_img1.y);

2 个答案:

答案 0 :(得分:0)

尝试此操作以获取图像视图的中心点。

int width=Bitmap.getWidth();
int height=Bitmap.getHeight();

int centerX=width/2;
int centerY=height/2;

答案 1 :(得分:0)

我使用MainActivity.java中的以下代码解决了问题。

PointF pointF_img1 = new PointF();
pointF_img1 = img_to_be_zoomed.getScrollPosition();
img_to_be_zoomed_mirror.setScrollPosition(1 - pointF_img1.x, pointF_img1.y);