gpt4 book ai didi

android - 如何将 View 扩展类的 Canvas 用于 Activity 类?

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:59 25 4
gpt4 key购买 nike

我创建了一个类:

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

现在我从 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);
}
}

现在我想将 Canvas 放入 Activity 类中并设置为 ImageView。我该怎么做?

最佳答案

您需要从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);
}
}


将自定义 View 添加到 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>


您的 Activity 类(class)中没有任何内容。就是这样!

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


引用和链接
查看类似问题:How to take canvas on imageview in android
简单的自定义 View 示例代码:Adding my custom View into an XML layout throws exception

阅读有关 Android 2D 绘图的信息:http://developer.android.com/guide/topics/graphics/2d-graphics.html
Android二维编码教程: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/

关于android - 如何将 View 扩展类的 Canvas 用于 Activity 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8122177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com