第一种:oncreate里启动自定义view


Activity代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity
{
    /**
     * 两种方法:
     * 1.直接在layout里找到自定义控件
     * 2.oncreate里启动自定义view
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
                                                                                                                                                                         
        //先new一个自定义view对象
        VerticalTextView view = new VerticalTextView(this);
        setContentView(view);
//        setContentView(R.layout.activity_main);
    }
                                                                                                                                                                     
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
                                                                                                                                                                     
}



View类代码:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class VerticalTextView extends View
{
    //定义一个自定义角度的文本
    private Paint mPaint;
    public VerticalTextView(Context context)
    {
        super(context);
        init();
    }
                                                                                                                                                    
    private void init()
    {
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setTextSize(50);
        mPaint.setAntiAlias(true);//设置抗锯齿
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
                                                                                                                                                        
        canvas.rotate(75);
//        canvas.rotate(60,bitmap.getWidth()*2 , bitmap.getHeight()*2);
        canvas.drawText("CanvasHello", 0, 0, mPaint);
        canvas.drawBitmap(bitmap, 0, 0, mPaint);
    }
                                                                                                                                                    
}



第二种:直接在layout里找到自定义控件(直接新建custom view)

1.新建attrs文件,增加自定义属性:

增加属性内容:





或者:





    
    
    

范例:


    
        
        
        
    


2.增加自己的命名空间,将android修改为自己的包名:

 
  


3.在layout中创建自定义view,设置自定义属性(控件包名记得修改):


范例:


    


4.在自定义view类中增加方法:

setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
if (textSize > 0) {
    setTextSize(textSize);
}


范例:

public class VerticalTextView extends View
{
    /**
     * 系统范例:LabelView
     * 1.新建attrs文件,增加属性:
        
        
        
        
        
        //////////////////////////////////////////////
        
        
        
        
            
            
            
        
        
        ////////////////////////////////////////////////
                            
        2.增加自己的命名空间,将android修改为自己的包名
        
                            
        4.在自定义view类中增加方法
        setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));
        int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
        if (textSize > 0) {
            setTextSize(textSize);
        }
                            
     */
    private Paint mPaint;
    private CharSequence s;
    private int c;
    private int d;
                        
                        
    public VerticalTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MyView);
        s = a.getString(R.styleable.MyView_mytext);
        c = a.getColor(R.styleable.MyView_mytextColor, 0xFF000000);
        d = a.getDimensionPixelOffset(R.styleable.MyView_mytextSize, 0);
        init();
        a.recycle();
    }
                        
    //定义画笔
    private void init()
    {
        mPaint = new Paint();
        mPaint.setColor(c );
        mPaint.setTextSize(d);
        mPaint.setAntiAlias(true);// 设置抗锯齿
    }
                        
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
                            
        canvas.rotate(30);
        // canvas.rotate(60,bitmap.getWidth()*2 , bitmap.getHeight()*2);
        canvas.drawText(s.toString(), 0, 0, mPaint);
        canvas.drawBitmap(bitmap, 0, 0, mPaint);
                            
    }
}



参考资料:

Android画图Path的使用(paint)

http://www.cnblogs.com/tt_mc/archive/2012/12/07/2807518.html