Android: Drawing on Canvas in Scrollview

旧时模样 提交于 2019-12-01 05:12:37

write this line in your touchevent of custom View class

getParent().requestDisallowInterceptTouchEvent(true)

finally I found solution to my problem. I used solution from this site:

Link1

well I saw that solution before, but unfortunately I didn't realize that this solution is good for me. I knew that I need onMeasure method for Canvas to show it in xml, but withouth of solution from mentioned site it didn't work. Now it works. Here is my xml, and SampleView solution.

XML code:

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

This is my SampleView solution:

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

    }
}

Now I got this image, when I run my app:

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

If anyone have questions about this feel free to ask me :).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!