android 自定义控件

本文介绍如何在Android应用中自定义控件并使用线程进行动画效果的实现,包括背景绘制、logo显示、进度条绘制及文本滚动等操作。

viewGroup;

五大控件都是view的子类;

这里我继承view;实现自定义控件;

java代码;


package com.example.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CustomView extends View implements Runnable{
    int viewWidth, viewHeight;
    Bitmap bitmapBackground,bitmapLogo,bitmapProgressBar;
    Thread thread;
    boolean isRun=true;
    int textLeft=0;
    int blueRectLeft,blueRectTop;
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        try {
            bitmapBackground = BitmapFactory.decodeResource(getResources(),
                    R.drawable.b);
            bitmapLogo = BitmapFactory.decodeResource(getResources(),
                    R.drawable.f);
            bitmapProgressBar = BitmapFactory.decodeResource(getResources(),
                    R.drawable.a);
            thread=new Thread(this);
            thread.start();
        } catch (Exception e) {
            // TODO: handle exception
        }
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(0xFFFF0000);
        Rect rect = new Rect(0, 0, viewWidth, viewHeight);
        canvas.drawRect(rect, paint);
        for (int row = 0; row < viewHeight / bitmapBackground.getHeight() + 1; row++) {
            for (int col = 0; col < viewWidth / bitmapBackground.getWidth() + 1; col++) {
                canvas.drawBitmap(bitmapBackground,
                        col * bitmapBackground.getWidth(), row
                        * bitmapBackground.getHeight(), paint);
            }
        }

        //画logo
        int logoLeft=(viewWidth-bitmapLogo.getWidth())/2;
        int logoTop=(viewHeight-bitmapLogo.getHeight())>>1;
            canvas.drawBitmap(bitmapLogo, logoLeft, logoTop, paint);

            //在logo下面画进度条
            int barLeft=(viewWidth-bitmapProgressBar.getWidth())>>1;
            int barTop=logoTop+bitmapLogo.getHeight()+10;
            canvas.drawBitmap(bitmapProgressBar, barLeft, barTop, paint);

            paint.setColor(0xFF0000FF);
            Rect blueRect=new Rect(blueRectLeft, 0, blueRectLeft+viewWidth, viewHeight);
            canvas.drawRect(blueRect, paint);

            canvas.drawText("我的梦是中国梦", textLeft, 100, paint);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        blueRectLeft=blueRectLeft+50;
        invalidate();
        return super.onTouchEvent(event);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        viewHeight = h;
        viewWidth = w;
        textLeft=w;
        blueRectLeft=-w;
    }

    @Override
    public void run() {
        while(isRun)
        {
            try {
                textLeft=textLeft-20;
                thread.sleep(100);
                //在主线程中用invalistate
                ///在工作线程中更新view
                this.postInvalidate();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }

    }

}

layout布局;

<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" >

    <com.example.view.CustomView
        android:id="@+id/msyview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值