绘图用帆布线从活动 [英] Drawing a line with canvas from an Activity

查看:107
本文介绍了绘图用帆布线从活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读以下链接:

<一个href=\"http://stackoverflow.com/questions/3616676/how-to-draw-a-line-in-android/12987068#12987068\">How画一条线在android系统

http://marakana.com/s/android_2d_graphics_example,1036/index.html

http://developer.android.com/reference/android/graphics/ Canvas.html

<一个href=\"http://stackoverflow.com/questions/6572905/activity-setcontentview-view-setcontentview\">Activity.setContentView, View.setContentView?

<一个href=\"http://stackoverflow.com/questions/11147704/how-to-create-an-activity-without-setcontentviewr-layout-main\">How创建无活动的setContentView(R.layout.main)

http://www.jayway.com/2009 / 3月26日/布局资源,在安卓/

我已阅读,谈论关于我的问题previous文章,但我解决不了我的问题。

I have read the previous articles that talk about about my problem but I can't solve my problem.

我的问题是我要画直线,以模拟图像之间的联系,但基本上是画直线。

My problem is that I want to draw straight lines to simulate the connection between images, but basically is to paint straight lines.

我见过的所有的例子都来自活动继承了主类,并有从View继承另一个内部类。并在主类中设置一个包含View类的用户界面,所以我们有一个空的用户界面。
事情是这样的:

All the examples I've seen have the main class that inherits from the activity and have another inner class that inherits from the View. And in the main class set the user interface that contains the View class, so we have a empty user interface. Something like this:

public class ActivityMain extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyView view1 =new MyView(this);
    setContentView(view1);       
}

public class MyView extends View {
    public MyView(Context c) {
        super(c);
    }
}
//And more code
}

我要定我的用户界面(XML文件)中,从活动继承(像往常一样)类:

I want to set my user interface (xml file) in the class that inherits from the activity (as usual):

public class ActivityMain extends Activity{
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.union);
}
}

和我从从活动继承绘制直线类想要的,我不希望有来自View继承任何类。

And I want from the class that inherits from activity to draw straight lines, I don't want to have any class that inherits from View.

我尝试以下,但我没有看到任何行:

I tried the following but I don't see any line:

public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();

switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        Canvas canvas = new Canvas();
        Paint paint = new Paint();
                    paint.setColor(Color.BLACK);
        canvas.drawLine(0, 0, 300, 700, paint);     
    break;
    case MotionEvent.ACTION_MOVE:
    break;
    case MotionEvent.ACTION_UP:
    break;
}   
return true;
}

在previous方法,我想从角落0,0画线到墙角300,700。这是一个简单的测试。但我不画任何东西,我不知道为什么。

In the previous method I want to draw a line from the corner 0,0 to the corner 300,700. It's a simple test. But I don't draw anything and I don't know why.

推荐答案

在这个环节我解释一下我的问题,我告诉我的解决办法<一href=\"http://stackoverflow.com/questions/12998774/load-a-layout-xml-file-in-a-view-class-android/13007764\">load在视图类的Andr​​oid 布局xml文件

in this link I explain my problem and I show my solution load a layout xml file in a View class android

我希望这种帮助他人=)

I hope this help other people =)

这篇关于绘图用帆布线从活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆