安卓:滚动型绘图画布画布

2023-09-07 16:02:03 作者:纯天然野生帅哥

我是pretty的编程中为Android太多新意。我的应用程序是由API演示在开发Android的网站示例应用程序。当我在样品图纸修改参数就变大。该绘图需要(它并不需要被收缩以适应屏幕),以显示在滚动视图。这是code我用:

I'm pretty much new in programing for Android. My app is sample app from api demos on developer android website. When I change parameters in that sample drawing it gets larger . That drawing needs to be displayed in scroll view (it doesn't need to be shrinked to fit the screen). This is the code I used:

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 code:

here is xml Code:

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布局或scrollviews绘图看起来像图1:

When I run that application and push the button in main activity application crashes. Drawing looks like Figure1 when I'm not using xml layout or scrollviews:

https://m.xsw88.com/allimgs/daicuo/20230907/6149.png          图1

https://m.xsw88.com/allimgs/daicuo/20230907/6149.png Figure1

我也试过用这种方法后的setContentView code:

I also tried to use this code after method setContentView:

View v = new SampleView(this);
addContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

也是这一个:

and also this one:

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

    addContentView(v, lp);

当我使用codeS,如上图所示,应用程序,图1显示没有滚动视图,它似乎是第二个内容视图将覆盖XML,但它不能正常显示。 从那以后,我试图用这个code的setContentView后:

when I use codes, shown above, app displays Figure1 without scrollview, it seems like second content view overwrites that xml but it doesn't show correctly. After that I tried to use this code after setContentView:

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

框布局(FrameLayout1)之后横向滚动视图中添加的routes.xml文件。当应用程序运行时,我得到的空白屏幕,不图1。 没有任何人有一个想法如何升级我的code,将在滚动型显示图1?

Frame layout (FrameLayout1) is added in routes.xml file after horizontal scroll view. When application runs I get blank screen without Figure1. Does Anyone have an idea how to upgrade my code that would display Figure1 in ScrollView?

在此先感谢!

推荐答案

在自定义视图类的TouchEvent写这行

write this line in your touchevent of custom View class

的getParent()。requestDisallowInterceptTouchEvent(真)

getParent().requestDisallowInterceptTouchEvent(true)