Android:在滚动视图中在画布上绘图

2023-11-21

我对 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代码:

路线.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 所示:

https://i.stack.imgur.com/za5MP.png Figure1

我还尝试在方法 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);

当我使用上面所示的代码时,应用程序显示图 1,但没有滚动视图,似乎第二个内容视图覆盖了该 xml,但它无法正确显示。 之后我尝试在 setContentView 之后使用此代码:

View v = new SampleView(this);
    FrameLayout fl = new FrameLayout(this);
    fl.findViewById(R.id.FrameLayout1);
    fl.addView(v);

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

提前致谢!


在自定义 View 类的 touchevent 中写入这一行

getParent().requestDisallowInterceptTouchEvent(true)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:在滚动视图中在画布上绘图 的相关文章

随机推荐