首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动滚动到Android TouchImageView中的左上角

自动滚动到Android TouchImageView中的左上角
EN

Stack Overflow用户
提问于 2015-06-12 09:40:55
回答 1查看 314关注 0票数 0

我在我的应用程序中使用了一个TouchImageView,其中我有一个比屏幕大小更大的图像。当我启动包含TouchImageView的活动时,它当前会自动显示屏幕中央的图像中心。我必须手动(使用拖动手势)来制造左上角的视觉效果。但是,默认情况下,我希望在屏幕的左上角显示图像的左上角。

我尝试了imgView.setScrollPosition(0,0),但没有任何结果。我还尝试将标度类型设置为“矩阵”,但这会放大图像,而我希望图像以原来的大小显示。Scaletypes、fitStart和fitEnd不受TouchImageView支持。

如何滚动到我的TouchImageView的左上角?

以下是我的XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.frankd.wttv.TouchImageView
        android:id="@+id/imageView"
        android:layout_width = "wrap_content"
        android:layout_height ="wrap_content"
        android:scaleType="matrix"/>

</LinearLayout>

下面是我如何打开布局和设置图像。

代码语言:javascript
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myLayout);

    //set timetable image
    TouchImageView imgView = (TouchImageView)findViewById(R.id.imageView);
    imgView.setImageResource(R.drawable.myImage);
    //TODO: scroll to top left corner of image
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-15 19:42:33

造成这个问题的原因是scrollToPosition(x,y)在TouchImageView中不使用x和y像素作为输入,而是一个0到1之间的数字,它反映了图像大小的比例。

此外,scrollToPosition(x,y)在TouchImageView的中心设置图像的一个点。因此,如果在scrollToPosition上调用TouchImageView (0.5),则图像的中心将显示在TouchImageView的中心。我们需要计算图像的哪个点需要放置在TouchImageView的中心,以使它很好地对齐。

我在TouchImageView.java中创建了函数TouchImageView.java (),只有在onMeasure()结束时才能从TouchImageView.java文件中调用它。如果您在前面调用它,getWidth()和getHeight()将返回0,因为视图还没有调整大小。

代码语言:javascript
复制
public void scrollToTopLeft() {
    try {
        float x, y, viewWidth, viewHeight, viewCenterX, viewCenterY, imageWidth, imageHeight;

        //these calls will only work if called after (or at the end of) onMeasure()
        viewWidth = this.getWidth();
        viewHeight = this.getHeight();

        // get center of view
        viewCenterX = viewWidth / 2;
        viewCenterY = viewHeight / 2;

        // get image height and width
        imageWidth = getImageWidth();
        imageHeight = getImageHeight();

        //calculate the x and y pixels that need to be displayed in at the center of the view
        x = viewWidth / imageWidth * viewCenterX;
        y = viewHeight / imageHeight * viewCenterY;

        //calculate the value of the x and y pixels relative to the image
        x = x / imageWidth;
        y = y / imageHeight;

        setScrollPosition(x, y);

    } catch (Exception E) {
        Log.v(TAG, E.toString());
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30799963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档