自定义开关按钮

效果图
自定义开关按钮_第1张图片
XML文件

<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"
    tools:context=".MainActivity" >

    <com.example.slidebtn.SlideBtton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

主Activity

package com.example.slidebtn;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

自定义view

package com.example.slidebtn;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/** * 创建自定义View的一般步骤 1.创建自定义view对象 构造方法 * 2.确定view的尺寸 onMeasure(intwidthMeasureSpec,int heightMeasureSpec) * 3.绘制view onDraw(Canvas canvas) * 4.确定在父View中的位置 onLayout() 通常我们并不关心这个方法 * */
public class SlideBtton extends View implements android.view.View.OnClickListener {
    /** * 确定在父view中的位置 * */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        // TODO Auto-generated method stub
        super.onLayout(changed, left, top, right, bottom);
    }

    private Bitmap bg, btn;
    private int bg_w, bg_h, btn_w;
    private Paint paint;
    private float start_offset;
    boolean isopen = false;

    /** * 加载布局文件时调用 * */
    public SlideBtton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 初始化资源
        inite();
    }

    private void inite() {
        bg = BitmapFactory.decodeResource(getResources(),
                R.drawable.switch_background);
        btn = BitmapFactory.decodeResource(getResources(),
                R.drawable.slide_button);
        bg_w = bg.getWidth();
        bg_h = bg.getHeight();
        btn_w = btn.getWidth();
        paint = new Paint();
        // 设置抗锯齿
        paint.setAntiAlias(true);
        setOnClickListener(this);
    }

    /** * 绘制View * */
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bg, 0, 0, paint);
        canvas.drawBitmap(btn, start_offset, 0, paint);
    }

    /** * 确定View的尺寸 * */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(bg_w, bg_h);
    }

    @Override
    public void onClick(View v) {
        isopen = !isopen;
        if (isopen) {

            start_offset = bg_w - btn_w;
        } else {
            start_offset = 0;
        }
        //通知重新绘制
        invalidate();
    }

}

你可能感兴趣的:(android,自定义)