【问题标题】:How to use canvas of view extended class into activity class?如何将视图扩展类的画布用于活动类?
【发布时间】:2011-11-14 13:18:29
【问题描述】:

我创建了一个类:

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

现在我从活动中调用该类:

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。我该怎么做?

【问题讨论】:

  • 您想将您的画布设置为某个ImageView 元素吗?还是只想在R.id.frameLayout1 中显示画布?
  • 我想把canvas设置成一个Imageview...
  • 您只想在特定视图内的屏幕上绘图吗?因为它不必是 ImageView,它可以是任何自定义视图。为什么需要它是ImageView?有什么特殊要求吗?

标签: android android-canvas


【解决方案1】:

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

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


如下所示,将自定义视图添加到 Activity 的 xml 布局中。

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


您的活动课程中没有任何内容。就是这样!

如果您确实需要使用 ImageView:从 ImageView 继承您的自定义视图,而不是 View。然后将活动布局 xml 中的适当 ImageView 标记替换为您的自定义 ImageView,例如 com.yourapphere.MyImageView)


参考和链接
查看类似问题:How to take canvas on imageview in android
简单的自定义视图示例代码: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/

【讨论】:

  • 现在,我如何从自定义视图中获取位图?
  • 如果它有助于解决您的问题,您能否点赞并接受这个答案?
【解决方案2】:

这是为遇到相同问题的人提供的解决方案

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

    Bitmap bitmap=Bitmap.createBitmap(440,587,Bitmap.Config.ARGB_8888);
    Canvas c=new Canvas(bitmap);
    tcanvas.draw(bitmap);

    //now i use bitmap at for any use.......

  }
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    相关资源
    最近更新 更多