Android:自定义View、Paint、Canvas、attrs、customview、path

简介:

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


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
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类代码:

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
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文件,增加自定义属性:

增加属性内容:

1
2
3
4
5
<declare-styleable name= "LabelView" >
<attr name= "text"  format= "string"  />
<attr name= "textColor"  format= "color"  />
<attr name= "textSize"  format= "dimension"  />
</declare-styleable>

或者:

1
2
3
4
5
6
7
8
9
<declare-styleable name= "DraggableDot" >
<attr name= "radius"  format= "dimension"  />
<attr name= "legend"  format= "string"  />
<attr name= "anr" >
     < enum  name= "none"  value= "0"  />
     < enum  name= "thumbnail"  value= "1"  />
     < enum  name= "drop"  value= "2"  />
</attr>
</declare-styleable>

范例:

1
2
3
4
5
6
7
<resources>
     <declare-styleable name= "MyView" >
         <attr name= "mytext"  format= "string"  />
         <attr name= "mytextColor"  format= "color"  />
         <attr name= "mytextSize"  format= "dimension"  />
     </declare-styleable>
</resources>


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

1
2
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:MyView= "http://schemas.android.com/apk/res/com.example.aexh23_paint_canvas"


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

1
2
3
4
5
6
7
8
9
<com.example.aexh23_paint_canvas.VerticalTextView
android:id= "@+id/verticalTextView1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
MyView:mytext= "HelloWorld"
MyView:mytextColor= "#654321"
MyView:mytextSize= "35sp"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"  />


范例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:MyView= "http://schemas.android.com/apk/res/com.example.aexh23_paint_canvas"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     tools:context= ".MainActivity"  >
     <com.example.aexh23_paint_canvas.VerticalTextView
         android:id= "@+id/verticalTextView1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         MyView:mytext= "HelloWorld"
         MyView:mytextColor= "#654321"
         MyView:mytextSize= "35sp"
         android:layout_centerHorizontal= "true"
         android:layout_centerVertical= "true"  />
</RelativeLayout>


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

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


范例:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public  class  VerticalTextView  extends  View
{
     /**
      * 系统范例:LabelView
      * 1.新建attrs文件,增加属性:
         <declare-styleable name="LabelView">
         <attr name="text" format="string" />
         <attr name="textColor" format="color" />
         <attr name="textSize" format="dimension" />
         </declare-styleable>
         //////////////////////////////////////////////
         <declare-styleable name="DraggableDot">
         <attr name="radius" format="dimension" />
         <attr name="legend" format="string" />
         <attr name="anr">
             <enum name="none" value="0" />
             <enum name="thumbnail" value="1" />
             <enum name="drop" value="2" />
         </attr>
         </declare-styleable>
         ////////////////////////////////////////////////
                             
         2.增加自己的命名空间,将android修改为自己的包名
         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:MyView="http://schemas.android.com/apk/res/com.example.aexh23_paint_canvas"
                             
         3.在layout中创建自定义view,增加属性
         <com.example.aexh23_paint_canvas.VerticalTextView
         android:id="@+id/verticalTextView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         MyView:mytext="HelloWorld"
         MyView:mytextColor="#654321"
         MyView:mytextSize="35sp"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true" />
                             
         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);
                             
     }
}





本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1228552,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
缓存 前端开发 Android开发
安卓开发中的自定义视图:从零到英雄
【10月更文挑战第42天】 在安卓的世界里,自定义视图是一块画布,让开发者能够绘制出独一无二的界面体验。本文将带你走进自定义视图的大门,通过深入浅出的方式,让你从零基础到能够独立设计并实现复杂的自定义组件。我们将探索自定义视图的核心概念、实现步骤,以及如何优化你的视图以提高性能和兼容性。准备好了吗?让我们开始这段创造性的旅程吧!
38 1
|
3月前
|
数据可视化 Android开发 开发者
安卓应用开发中的自定义View组件
【10月更文挑战第5天】在安卓应用开发中,自定义View组件是提升用户交互体验的利器。本篇将深入探讨如何从零开始创建自定义View,包括设计理念、实现步骤以及性能优化技巧,帮助开发者打造流畅且富有创意的用户界面。
128 0
|
2月前
|
XML 前端开发 Android开发
Android:UI:Drawable:View/ImageView与Drawable
通过本文的介绍,我们详细探讨了Android中Drawable、View和ImageView的使用方法及其相互关系。Drawable作为图像和图形的抽象表示,提供了丰富的子类和自定义能力,使得开发者能够灵活地实现各种UI效果。View和ImageView则通过使用Drawable实现了各种图像和图形的显示需求。希望本文能为您在Android开发中使用Drawable提供有价值的参考和指导。
49 2
|
2月前
|
搜索推荐 前端开发 Android开发
安卓应用开发中的自定义视图实现
【10月更文挑战第30天】在安卓开发的海洋中,自定义视图是那抹不可或缺的亮色,它为应用界面的个性化和交互体验的提升提供了无限可能。本文将深入探讨如何在安卓平台创建自定义视图,并展示如何通过代码实现这一过程。我们将从基础出发,逐步引导你理解自定义视图的核心概念,然后通过一个实际的代码示例,详细讲解如何将理论应用于实践,最终实现一个美观且具有良好用户体验的自定义控件。无论你是想提高自己的开发技能,还是仅仅出于对安卓开发的兴趣,这篇文章都将为你提供价值。
|
2月前
|
Android开发 开发者 UED
安卓开发中自定义View的实现与性能优化
【10月更文挑战第28天】在安卓开发领域,自定义View是提升应用界面独特性和用户体验的重要手段。本文将深入探讨如何高效地创建和管理自定义View,以及如何通过代码和性能调优来确保流畅的交互体验。我们将一起学习自定义View的生命周期、绘图基础和事件处理,进而探索内存和布局优化技巧,最终实现既美观又高效的安卓界面。
46 5
|
3月前
|
缓存 数据处理 Android开发
在 Android 中使用 RxJava 更新 View
【10月更文挑战第20天】使用 RxJava 来更新 View 可以提供更优雅、更高效的解决方案。通过合理地运用操作符和订阅机制,我们能够轻松地处理异步数据并在主线程中进行 View 的更新。在实际应用中,需要根据具体情况进行灵活运用,并注意相关的注意事项和性能优化,以确保应用的稳定性和流畅性。可以通过不断的实践和探索,进一步掌握在 Android 中使用 RxJava 更新 View 的技巧和方法,为开发高质量的 Android 应用提供有力支持。
|
3月前
|
缓存 调度 Android开发
Android 在子线程更新 View
【10月更文挑战第21天】在 Android 开发中,虽然不能直接在子线程更新 View,但通过使用 Handler、AsyncTask 或 RxJava 等方法,可以实现子线程操作并在主线程更新 View 的目的。在实际应用中,需要根据具体情况选择合适的方法,并注意相关的注意事项和性能优化,以确保应用的稳定性和流畅性。可以通过不断的实践和探索,进一步掌握在子线程更新 View 的技巧和方法,为开发高质量的 Android 应用提供支持。
53 2
|
3月前
|
XML 前端开发 Android开发
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
|
3月前
|
XML 前端开发 Android开发
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
31 2
|
Android开发
自定义android 4.0以上的对话框风格
做个笔记,这里是Dialog的风格,如果是用AlertDialog创建的,不能直接用。在styles.xml的写法: 22sp @color/font_green 1 true @st...
712 0