当前位置:操作系统 > 安卓/Android >>

android通过canvas旋转,绘制文字,竖直等不同方向的显示

 效果如下:

主体代码如下:
 

package com.free.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;

public class DrawTextStudy extends Su易做图ceView implements Su易做图ceHolder.Callback {

    Su易做图ceHolder holder;
    public DrawTextStudy(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        holder = this.getHolder();
        holder.addCallback(this);
    }

    @Override
    public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void su易做图ceCreated(Su易做图ceHolder holder) {
        // TODO Auto-generated method stub
        new Thread(new MyThread()).start();
    }

    @Override
    public void su易做图ceDestroyed(Su易做图ceHolder holder) {
        // TODO Auto-generated method stub
       
    }
   
    void drawText(Canvas canvas ,String text , float x ,float y,Paint paint ,float angle){
        if(angle != 0){
            canvas.rotate(angle, x, y);
        }
        canvas.drawText(text, x, y, paint);
        if(angle != 0){
            canvas.rotate(-angle, x, y);
        }
    }
   
    class MyThread implements Runnable{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Canvas canvas = null;   
            try{                       
                canvas = holder.lockCanvas();
                Paint paint = new Paint();               
                paint.setColor(Color.WHITE);
                paint.setTextSize(20);               
                canvas.drawLine(100, 100, 100, 400, paint);
                drawText(canvas,"Hello", 80, 200, paint,-90);       
               
                paint.setColor(Color.RED);
                paint.setTextSize(40);
                drawText(canvas,"free", 150, 180, paint,-45);       
               
                paint.setColor(Color.BLUE);
                drawText(canvas,"World", 150, 80, paint,0);
                canvas.drawLine(100, 100, 400, 100, paint);
               
            } catch(Exception e){
               
            } finally {
                holder.unlockCanvasAndPost(canvas);
            }       
           
        }       
    }
   

}

 

activity调用

 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(new DrawTextStudy(this));
}

 

 

摘自 freeliver54

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,