Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

performance - Sluggish zoom and scroll with GridView in Android

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);
}

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?

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

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);  
}

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);
    }
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...