如何使用视图扩展类的画布进入活动类? [英] How to use canvas of view extended class into activity class?

查看:102
本文介绍了如何使用视图扩展类的画布进入活动类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个班级:

public class TestCanvas extends View {

    public TestCanvas(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);

        canvas.drawText("kal", 0, 100, paint);
        canvas.save();
    }
}

现在我从活动中调用该班级

Now I call that class from activity:

public class TestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TestCanvas tcanvas=new TestCanvas();

        frameLayout=(FrameLayout)findViewById(R.id.frameLayout1);
        frameLayout.addView(tcanvas);   
    }
}

现在,我想将画布放入活动类并设置为ImageView.我该怎么办?

Now I want to get canvas into activity class and set to ImageView. How would I do this?

推荐答案

您需要从View继承自己的类,并覆盖onDraw()onMeasure().就像您开始使用TestCanvas一样.一个例子:

You need to inherit your own class from View and override onDraw() and onMeasure(). Like you started to do with TestCanvas. An example:

package com.yourapphere;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class TwoDee extends View {
    private int mWidth;
    private int mHeight;


    public TwoDee(Context context) {
        super(context);
    }

    public TwoDee(Context context, AttributeSet attribs) {
        super(context, attribs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint(); 
        paint.setColor(Color.GRAY); 
        paint.setStyle(Style.FILL); 
        canvas.drawPaint(paint);

        paint.setColor(Color.BLUE);
        canvas.drawLine(0, 0, mWidth, mHeight, paint);
        canvas.drawLine(mWidth, 0, 0, mHeight, paint);

        paint.setColor(Color.RED);
        canvas.drawText("0 kal", 50, 85, paint);
        canvas.save();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
        mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }
}


将自定义视图添加到活动的xml布局中,如下所示.


Add your custom view into your activity's xml layout as below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
        />
        <com.yourapphere.TwoDee
            android:layout_width="150dp"
            android:layout_height="100dp"
        />
</LinearLayout>


您的活动课堂上没有任何内容.就是这样!


Nothing goes into your activity class. That's it!

如果您确实需要使用ImageView:从ImageView而不是View继承您的自定义视图.然后,使用自定义ImageView(如com.yourapphere.MyImageView)

If you really need to use ImageView: inherit your custom view from ImageView instead of View. Then replace appropriate ImageView tags in your activity's layout xml with your custom ImageView like com.yourapphere.MyImageView)


参考文献和链接
看到类似的问题:如何在android中的imageview上绘制画布
简单的自定义视图示例代码:将我的自定义视图添加到XML布局中会引发异常


References & links
See similar question: How to take canvas on imageview in android
Simple custom view example code: Adding my custom View into an XML layout throws exception

了解有关Android 2D绘图的信息: http://developer.android .com/guide/topics/graphics/2d-graphics.html
Android 2D编码教程: http://www3.ntu.edu .sg/home/ehchua/programming/android/Android_2D.html
简单的2D游戏教程: http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/

Read about Android 2D drawing: http://developer.android.com/guide/topics/graphics/2d-graphics.html
Android 2D coding tutorial: http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_2D.html
Simple 2D game tutorial: http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/

这篇关于如何使用视图扩展类的画布进入活动类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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