在 Android 中使用 GridView 缓慢缩放和滚动 [英] Sluggish zoom and scroll with GridView in Android

查看:16
本文介绍了在 Android 中使用 GridView 缓慢缩放和滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个派生自 GridView 的自定义视图.这包含具有缩放和平移功能的自定义 ImageView.滚动和缩放功能缓慢.如果父视图是相同的自定义 ImageView 而不是 GridView 它完美地工作.我正在尝试这样做,因为我需要一个静态背景图像,中间有一个交互式区域.缩放和滚动的实现类似于 这篇文章.我上传了一个重现问题的测试项目这里.OnCreate 方法是这样实现的:

I'm creating a custom view derived from GridView. This contains a custom ImageView with zooming and panning functionality. Scroll and zoom functionality is sluggish. If the parent view is the same custom ImageView instead of a GridView it works perfectly. I'm trying to do this because I need a static background image with an interactive area at the center. Zoom and scroll are implemented similar to this article. I uploaded a test project reproducing the problem here. The OnCreate method is implemented like this:

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

    View myView1;
    boolean sluggish = false;

    if (sluggish) {
        myView1 = new MyGridView(this);
    }
    else {
        myView1 = new MyImageView(this);        
    }

        setContentView(myView1);
}

如果您将 sluggish 变量设置为 true,您会发现 ImageView 对此效果更好.您知道为什么使用 GridView 会导致缩放和滚动无法使用或如何解决吗?

If you set the sluggish variable to true you'll see ImageView works much better for that. Do you have any idea why using GridView turns zoom and scroll unusable or how can this be solved?

我没有将动态"ImageView 用于静态"ImageView,因为我还没有找到如何将 ImageView 嵌入到另一个 ImageView 中.

I'm not using a "dynamic" ImageView into a "static" ImageView because I haven't found how to embed an ImageView into another ImageView.

提前致谢.

推荐答案

目前我发现的最简单、最简单的解决方案是使用自定义 FrameLayout:

The simplest and easiest solution I found so far is using a custom FrameLayout:

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

    View myView1 = new MyFrameLayout(this);

    setContentView(myView1);  
}

实现如下所示,并使用我发布的第一个示例中的 MyImageView 自定义控件.

Which is implemented as shown below and uses the MyImageView custom control in the first example I posted.

public class MyFrameLayout extends FrameLayout {

    public MyFrameLayout(Context context) {
        super(context);

        MyImageView i1 = new MyImageView(context);

        this.addView(i1);


        MyImageView i2 = new MyImageView(context);

        LayoutParams lp = new LayoutParams(300, 300);
        lp.leftMargin = 100;
        lp.topMargin = 100;
        lp.gravity = 0;

        this.addView(i2, lp);
    }
}

我从 Lawrence D'Olivero 通过 twitter 获得的替代解决方案建议.它包括子类化 View、缓存它和使用 OnDraw 事件 来合成滚动图像在固定背景上作为他的演示这里.

An alternative solution suggestion I got via twitter from Lawrence D'Olivero. It consists on subclassing View, caching it and using the OnDraw event for composing the scrolling image on a fixed background as his demo here.

这篇关于在 Android 中使用 GridView 缓慢缩放和滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆