English 中文(简体)
我怎样才能在画布上画画?
原标题:How can I paint with canvas in a Scrollview?

I have a canvas, in which I want to paint several rectangles, I can paint with canvas in an InternalView, but I need to paint in a scrollview, I tried with this code, but some have evil. This is the code:

   package prueba.android.ondraw;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Prueba_ondrawActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        setContentView(R.layout.main);

        MyView v = new MyView(this);
        layout.addView(v);

    }
    private class MyView extends View{
        public MyView(Context context) {
            super(context);
        }
        protected void onDraw(Canvas canvas) {
            int j = 0;
            for(int i=0;i<10;i++){
                super.onDraw(canvas);
                Paint paint = new Paint();
                paint.setColor(Color.CYAN);
                paint.setAntiAlias(true);
                canvas.drawRect(0, j,50,50, paint);
                j= j+100;
            }
        }
    }
}

主线. xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@android:color/darker_gray">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="150dp"
        android:layout_height="400dp"
        android:layout_marginLeft="100dp" >
        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </LinearLayout>
    </ScrollView>

</LinearLayout>

帮帮我,谢谢!

最佳回答

You are doing some strange things in your code:
first you try to find a view (findViewById()) before you have used setContentView() (sv will always be null here, because there are no views yet), then you replace that View with a new ScrollView (which you don t use), and finally you set the content to a layout.

相反,你可能希望 想要 做的是创建你自己的视图, 然后在滚动视图中使用它?

在您的 ScrollView 中, 您有一个布局, 这将需要一个 ID :

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="150dp"
    android:layout_height="400dp"
    android:layout_marginLeft="100dp" >
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</ScrollView>

然后在代码中找到这个布局 :

setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
//Create a new instance of MyView...
MyView v = new MyView(this);
//...And add it to the layout:
layout.addView(v);

您可以像这样定义您自己的 MyView 定义 :

private class MyView extends View{
    //Your onDraw() method here.
}

(通知我称自定义视图为 < code> MyView 。 这是因为“ roid” 已经有一个名为 < code> View 的类, 所以如果给出已经存在的类名, 可能会引起很多混乱 )

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签