Android:在ScrollView上绘制Canvas

8

我是一名新手,刚开始接触Android编程。我的应用程序是从开发者Android网站上的API演示中获取的样例应用程序。当我更改该样例绘图中的参数时,它会变得更大。这个绘图需要在滚动视图中显示(不需要缩小以适应屏幕)。以下是我使用的代码:

DrawPoints.java

    public class DrawPoints extends myActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.routes); 

    }

    public static class SampleView extends View {
        private Paint   mPaint = new Paint();
        private float[] mPts;

       /*here comes declaration of parametars



        private void buildPoints() {

        /*here comes some coding*/

            }
        }

        public SampleView(Context context, AttributeSet attributeset) {
            super(context, attributeSet);

            buildPoints();
        }

        @Override 
        protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;

            //here also comes code
        }
    }
}

以下是 XML 代码:

routes.xml

<?xml version="1.0" encoding="utf-8"?>
    <ScrollView 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/scrollView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        <HorizontalScrollView 
                android:id="@+id/scrollView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                    <!-- This code is just to make sure that scroll views work how 
                        I want them to work, image size is 625*351 px
                     <ImageView
                        android:id="@+id/image_View1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:src="@drawable/bus_design"
                            /> -->
                     <my.package.DrawPoints.SampleView
                        android:layout_width="fill_parent" 
                        android:layout_height="fill_parent" /> 


        </HorizontalScrollView>
    </ScrollView>

当我运行该应用程序并在主活动中按下按钮时,应用程序会崩溃。如果我不使用xml布局或滚动视图,则绘制看起来像图1所示: Figure1 图1 我也尝试在setContentView方法后使用以下代码:
View v = new SampleView(this);
addContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

还有这个:

View v = new SampleView(this);
    ScrollView.LayoutParams lp = new ScrollView.LayoutParams(1000, 1000);

    addContentView(v, lp);

当我使用上面展示的代码时,应用程序显示Figure1而没有滚动视图,似乎第二个内容视图覆盖了XML,但它并没有正确显示。 之后,我尝试在setContentView之后使用以下代码:
View v = new SampleView(this);
    FrameLayout fl = new FrameLayout(this);
    fl.findViewById(R.id.FrameLayout1);
    fl.addView(v);

在水平滚动视图之后的routes.xml文件中添加Frame布局(FrameLayout1)。当应用程序运行时,我得到了一个没有Figure1的空白屏幕(见图1)。 有人知道如何升级我的代码以在ScrollView中显示Figure1吗?
提前感谢!

大家好,没有人回答我的问题,所以我寻找了一些解决方案。我发现在Android中没有实现双向滚动。我在这个网页上找到了一个非常好的双向滚动实现[链接]https://dev59.com/snI-5IYBdhLWcg3wBjrl,Mahdi Hijazi发布了这个解决方案。这个解决方案非常好用,但是当我尝试将我的SampleView类应用到他的解决方案时,什么也没有发生。如果我找到了解决方案,我会在这里发布。在此之前,希望有人能提供解决方案。 - Kristijan
2个回答

12

在自定义视图类的触摸事件中编写此行代码:

getParent().requestDisallowInterceptTouchEvent(true)

(该代码可防止父视图拦截子视图的触摸事件)

1
太棒了 :) 到目前为止最好的解决方案,谢谢!我只是在 ScrollView 父级上调用了这个。 - JWqvist

5
最终我找到了解决问题的方法。我使用了这个网站提供的解决方案:Link1
我之前看过这个解决方案,但不幸的是我没有意识到这个解决方案适合我。我知道我需要Canvas的onMeasure方法来在xml中显示它,但如果没有上述网站提供的解决方案,它就行不通。现在它可以工作了。以下是我的xml和SampleView解决方案。 XML代码:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_layout" android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <!-- SCENE -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scene_layout" 
        android:drawingCacheQuality="low"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <com.testiranje.Kristijan.TwoDScrollView
            android:id="@+id/scene_scroller" android:drawingCacheQuality="low"
            android:scrollbars="horizontal"

            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/scene_container" 
                android:drawingCacheQuality="low"
                android:background="@drawable/map"
                android:layout_width="fill_parent" android:layout_height="fill_parent">

                <com.testiranje.Kristijan.SampleView
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent">

                    </com.testiranje.Kristijan.SampleView>

                <!--  <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/scene_background" android:drawingCacheQuality="low"
                    android:background="@drawable/map"
                    android:layout_width="fill_parent" android:layout_height="fill_parent" /> -->
            </RelativeLayout>
        </com.testiranje.Kristijan.TwoDScrollView>
    </RelativeLayout>
</RelativeLayout>
这是我的SampleView解决方案:
 public class SampleView extends View {
    private Paint   mPaint = new Paint();
    private float[] mPts;

    private static final float SIZE = 1000;
    private static final int SEGS = 50;
    private static final int X = 0;
    private static final int Y = 1;

    private void buildPoints() {
        final int ptCount = (SEGS + 1) * 2;
        mPts = new float[ptCount * 2];

        float value = 0;
        final float delta = SIZE / SEGS;
        for (int i = 0; i <= SEGS; i++) {
            mPts[i*4 + X] = SIZE - value;
            mPts[i*4 + Y] = 0;
            mPts[i*4 + X + 2] = 0;
            mPts[i*4 + Y + 2] = value;
            value += delta;
        }
    }

    public SampleView(Context context){
        super(context);
        //initSampleView();
        buildPoints();

    }

   //This constructor is very important because withouth of this 
   //you can't insert this view in xml
   public SampleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //initSampleView();
        buildPoints();
    }

    /*private final void initSampleView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        setPadding(3, 3, 3, 3);
    }*/

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    /**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        //This is because of background image in relativeLayout, which is 1000*1000px
        measureSpec = 1001;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.UNSPECIFIED) {
            // We were told how big to be
            result = specSize;
        }

        return result;
    }

    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
   private int measureHeight(int measureSpec) {
        int result = 0;
        //This is because of background image in relativeLayout, which is 1000*1000px 
        measureSpec = 1001;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);


        if (specMode == MeasureSpec.UNSPECIFIED) {
            // Here we say how Heigh to be
            result = specSize;
        } 
        return result;
    }

    @Override 
    protected void onDraw(Canvas canvas) {
        Paint paint = mPaint;

        canvas.translate(10, 10);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(0);
        canvas.drawLines(mPts, paint);


        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(3);
        canvas.drawPoints(mPts, paint);

    }
}

现在我运行应用程序后得到了这张图片:

http://i.stack.imgur.com/lWhvT.png

如果有任何问题,请随时问我:)。


如何在画布上添加滚动条? - AnAndroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接