2011-11-14 67 views
3

我创建一个类:如何将视图扩展类的画布用于活动类?

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的。我将如何做到这一点?

+0

谁做一些成'ImageView'元素的解决方案吗?或者你只是想在'R.id.frameLayout1'中显示画布? –

+0

我想将画布设置为一个Imageview ... – Kalpesh

+0

您是否只想在特定视图内的屏幕上绘制?因为它不一定是一个ImageView,它可以是任何自定义视图。为什么你需要它成为ImageView?这有什么特别的要求吗? –

回答

5

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


添加自定义视图到您的活动的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标签与像com.yourapphere.MyImageView您的自定义的ImageView)


参考&链接
见类似的问题: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/

+0

现在,我如何从自定义视图中获取位图? – Kalpesh

+0

好的....好的...我知道了...谢谢你,非常感谢你,先生...詹姆斯先生,你好吗? – Kalpesh

+0

如果能帮助解决你的问题,请你注册并接受这个答案吗? –

1

这里是为那些有同样的问题,你要设置你的画布

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

    } 
} 
相关问题