zoukankan      html  css  js  c++  java
  • Android自定义View

    Android自定义View实现很简单:

    1、继承View,重写构造函数、onDraw,(onMeasure)等函数。

    2、如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。

    3、在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".

    4、在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。

    实例:

    自定义TextView类:

    package com.zst.service.component;
    
    import com.example.hello_wangle.R;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    
    public class MyTextView extends TextView {
        //不能在布局文件中使用
        public MyTextView(Context context) {
            super(context);
        }
        
        //布局文件中用到此构造函数
        public MyTextView(Context content, AttributeSet attrs){
            super(content, attrs);
            Paint paint = new Paint();
            TypedArray array = content.obtainStyledAttributes(attrs, R.styleable.MyView);
            int color = array.getColor(R.styleable.MyView_textColor, 0xFF00FF00);
            float size = array.getDimension(R.styleable.MyView_textSize, 36);
            paint.setColor(color);
            paint.setTextSize(size);
            Log.i("MyTextView", "color:" + color + "	, size:" + size);
            
            array.recycle();
        }
    
        //修改背景颜色
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            canvas.drawColor(Color.YELLOW);
        }
    
    }

    自定义计数器View:

    package com.zst.service.component;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.View.OnClickListener;
    
    public class CounterView extends View implements OnClickListener{
        private Paint paint;
        private Rect bounds;
        private int mCount;
        
        public CounterView(Context context, AttributeSet attrs) {
            super(context, attrs);
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            bounds = new Rect();
            setOnClickListener(this);
        }
        
        @Override
        public void onClick(View v) {
            mCount++;
            invalidate(); //导致视图重绘,会调用onDraw方法
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            paint.setColor(Color.BLUE);
            canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
            paint.setColor(Color.RED);
            paint.setTextSize(30);
            String text = mCount + "";
            paint.getTextBounds(text, 0, text.length(), bounds);
            int textWidth = bounds.width();
            int textHeight = bounds.height();
            canvas.drawText(text, getWidth()/2-textWidth/2, getHeight()/2+textHeight/2, paint);
            
        }
    
    }

    相应的属性文件attrs.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="MyView">
            <attr name="textColor" format="color" />
            <attr name="textSize" format="dimension" />
            <attr name="textValue" format="string" />
        </declare-styleable>
    </resources>

    在布局文件中使用activity_new.xml:

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:my="http://schemas.android.com/apk/res/com.example.hello_wangle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        
    <TextView android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/definedTextView"
             android:text="@string/app_name"/>
    <com.zst.service.component.CounterView 
            android:layout_width="100dp"
            android:layout_height="100dp"
            />
        
    <com.zst.service.component.MyTextView      
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"    
           my:textColor="#FFFFFFFF"    
           my:textSize="38sp"  
        />  
    
        
    
    </LinearLayout> 

     NewViewActivity.java:

    package com.example.hello_wangle;
    import android.app.Activity;
    import android.os.Bundle;
    
    public class NewViewActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_new);
            //setContentView(new MyTextView(this));
            setContentView(R.layout.activity_new);
        }
    }

     

  • 相关阅读:
    GIT → 04:Git与代码托管平台
    GIT → 03:Git的下载和安装
    GIT → 02:Git和Svn比较
    GIT → 01:学习版本控制的原因
    GIT → 00:GIT学习大纲
    GIT → 10:基于IntelliJ IDEA的Git 操作
    GIT → 11:Git 工作流与实战演练
    GIT → 09:TortoiseGit 图形化工具
    亚马逊服务器搭建pptp方法
    Safari获取UDID需要安装.mobileconfig文件,
  • 原文地址:https://www.cnblogs.com/wangle1001986/p/3933310.html
Copyright © 2011-2022 走看看