UI (1) 简单的画布

本文详细介绍了在Android中如何使用View进行自定义绘图,包括创建自定义View的基本步骤、如何利用Canvas绘制基本图形如矩形、圆形和文本等。同时,文章还探讨了绘画过程中的一些高级技巧,例如旋转和平移等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

画画 可以在 view 和SurfaceView上面画

SurfaceView继承view 更加平滑 适合动画


在view上画画

最简单的 需要什么呢:

1 得到画布:继承view实现onDraw()在他里面画画了。他决定了 你画的是什么形状 比如 菱形椭圆等待呢个

2得到画笔:设置Paint (画笔)  决定了你是 用铅笔 毛笔等等  决定粗细 颜色 硬度等等

    其中有一个函数叫Invalidate()

    作用是刷新 画完刷一下 开始画 。可以画出闪烁等样子了。。

    如果在子线程中 就要用postInvalidate()方法了。这个等于发送一个handler

    在主线程中更新界面


ui都是由view和viewGroup组成的

view理解为各种控件

viewGroup理解为LinearLayout 等各种布局 。。。

空间要在布局里面才可以显示 不然就乱了。。

所以android定义的是 

                    ViewGroup

        ViewGroup                view

view        view                view  view

这个结构就可看出 所有要定义 控件先定义一个布局

在界面上定义一个Butten按钮方法两种

1动态定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example.view;
 
import java.util.concurrent.ConcurrentHashMap;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
 
public class CustomViewActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
 
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT)
 
        );
        Button button = new Button(this);
        button.setText("wang button");
        button.setLayoutParams(new LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
 
 
        setContentView(linearLayout);
    }
}

一般都是 setContentView(R.layout.xx);的形式 但xml和LinearLayout都是view所以也可以

这样就 完全没有xml定义了button。

1 xml定义

text.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
 
    <Button
        android:id="@+id/AA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
 
</RelativeLayout>

在activity中setContentView(linearLayout);改为setContentView(R.layout.text);

下面就画一个简单的 红色正方形 写上字 有监听事件的

activity中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.view;
 
import java.util.concurrent.ConcurrentHashMap;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
 
public class CustomViewActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
 
 
        CostomView cv = new CostomView(this);
        cv.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
 
                Toast.makeText(CustomViewActivity.this"hello word"1).show();
            }
        });
        setContentView(cv);
    }
}

CostomView中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.view;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
 
public class CustomView extends View {
 
    private Paint paint;
 
    public CustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
     
        paint=new Paint();
        paint.setColor(Color.RED);
         
         
        //drawRect
        canvas.drawRect(10109090, paint);
         
//      canvas.clipRect(20, 50, 180, 170);//裁剪
        canvas.rotate(40f);
        canvas.save();
         
        //drawText
        paint.setColor(Color.BLACK);
        canvas.drawText("helloview"2020, paint);
        //drawOval
        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);
         
        RectF oval = new RectF(2010100120);
        canvas.drawOval(oval, paint);
        canvas.restore();
//      canvas.setMatrix(matrix);
 
    }
}




其中canvas.save()保存斜的画布 为正常的。。canvas.restore();取出保存的内容 。感觉很难理解。。

我们如果单独处理一张图片的时候,而且不想影响其他部分的绘制

上面代码 就让 红色的方形 不懂 文字和椭圆 转了


用canvas画图

常用的draw****

drawRect 长方形 矩形

drawCircle 园

drawOval椭圆

drawPath 路径  不规则图形

drawLine    一条线

drawPoint    一个点

drawText        画一个文字

drawColor    画一个背景色

drawBitmap 画一个图片


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值