为了账号安全,请及时绑定邮箱和手机立即绑定

刚学习安卓几个月,自己写了一个简单的自定义View

标签:
Android

实现流星雨的效果,第一次发,还望有大神看到能指点一下

不会描述,具体代码大家看一下

这是Activity代码:

主视图 用于加载布局以及实现Meteor视图结束的回调

?

01

02

03

04

05

06

07

08

09

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

package com.hy.rpg;

 

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.RelativeLayout;

 

import com.hy.rpg.view.MeteorView;

import com.hy.rpg.view.MeteorView.OnViewStopListener;

 

 

public class MainActivity   extends Activity   implements OnViewStopListener   {

 

    private static final String   TAG = <span style="font-size: 9pt; line-height: 25.2px;">MainActivity</span><span   style="font-size: 9pt; line-height: 1.8em;">.class.getSimpleName();</span><br   data-filtered="filtered">    protected RelativeLayout layout;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        layout   = new RelativeLayout(this);

        layout.setBackgroundResource(R.mipmap.about_bg);

        initLayout();

        setContentView(layout);

    }

 

    private void initLayout() {

        for (int i = 0; i < 10; i++) {

            MeteorView   view = new MeteorView(this);

            view.setOnViewStopListener(this);

            layout.addView(view);

        }

    }

 

    @Override

    protected void onPause() {

        super.onPause();

    }

 

    @Override

    protected void onDestroy() {

        super.onDestroy();

        layout.removeAllViews();

    }

 

    @Override

    public void viewStop(View view) {

        layout.removeView(view);

        Log.e(TAG,   "" +   layout.getChildCount());

        if (layout.getChildCount() != 0) {

            MeteorView   meteorView = new MeteorView(this);

            meteorView.setOnViewStopListener(this);

            layout.addView(meteorView);

        }

    }

}

 

这是自定义View:

视图内容为一个图片从屏幕随机的位置向左下移动,多个视图加上背景就有了流星雨的效果了

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

package com.hy.rpg.view;

 

import android.content.Context;

import android.content.res.Resources;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.os.Handler;

import android.util.AttributeSet;

import android.view.View;

 

import com.hy.rpg.R;

import com.hy.rpg.utils.Utils;

 

/**

 * meteor view

 *

 * @author HY

 */

@SuppressWarnings("ALL")

public class MeteorView   extends View {

 

    /*message of view   stop*/

    private static   final int VIEW_STOP = 0;

 

    /*screen size*/

    protected int   width, height;

    /*X and Y   coordinate*/

    protected int   coordinateX, coordinateY;

    /*Y coordinate   max value*/

    protected int   maxY;

    protected Paint   mPaint;

    protected Bitmap   bitmap;

 

    protected   Resources res;

    /*handle the   massage when this view will stop*/

    private Handler   mHandler = new Handler() {

        public   void handleMessage(android.os.Message msg) {

            listener.viewStop(MeteorView.this);

        }

    };

 

    public   MeteorView(Context context) {

        this(context,   null);

    }

 

    public   MeteorView(Context context, AttributeSet attrs) {

        this(context,   attrs, 0);

    }

 

    public   MeteorView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context,   attrs, defStyleAttr);

        res   = this.getResources();

        height   = Utils.getHeight(res);

        width   = Utils.getWidth(res);

        maxY   = height - Utils.getDpSize(100, res);

        /*generate   random coordinate in screen*/

        coordinateX   = Utils.getRanNum(width * 3 / 4) + (width / 4);

        coordinateY   = Utils.getRanNum(height / 2);

        mPaint   = new Paint();

        bitmap   = BitmapFactory.decodeResource(res, R.mipmap.ic_meteor);

        new   MeteorThread().start();

    }

 

    @Override

    protected void   onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvas.drawBitmap(bitmap,   coordinateX, coordinateY, mPaint);

    }

 

    class   MeteorThread extends Thread {

        @Override

        public   void run() {

            boolean   flag = true;

            int   rate = CommonUtils.getRanNum(7) + 3;

            while   (flag) {

                coordinateX   -= rate;

                coordinateY   += rate;

                if   (coordinateX <= 0 || coordinateY >= maxY) {

                    flag   = false;

                    mHandler.sendEmptyMessage(VIEW_STOP);

                }

                try   {

                    Thread.sleep(20);

                }   catch (InterruptedException e) {

                    e.printStackTrace();

                }

                postInvalidate();

            }

        }

    }

 

    protected   OnViewStopListener listener;

    public void   setOnViewStopListener(OnViewStopListener listener) {

        this.listener   = listener;

    }

 

    /**

     * view stop   listener

     *

     * @author   HY

     */

    public interface OnViewStopListener {

        void viewStop(View view);

    }

}

对应的工具类:

用于获取屏幕尺寸等

?

01

02

03

04

05

06

07

08

09

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

package com.hy.rpg.utils;

 

import android.content.res.Resources;

import android.util.DisplayMetrics;

 

import java.util.Random;

 

/**

 * Created by HY on 2016/12/27.

 * get phone info

 */

 

public class Utils   {

    /**

     * get   window width

     *

     * @param   res resource

     * @return   window width

     */

    public static int getWidth(Resources   res) {

        DisplayMetrics   outMetrics = res.getDisplayMetrics();

        return outMetrics.widthPixels;

    }

 

    /**

     * get   window height

     *

     * @param   res resource

     * @return   window height

     */

    public static int getHeight(Resources   res) {

        DisplayMetrics   outMetrics = res.getDisplayMetrics();

        return outMetrics.heightPixels;

    }

 

    /**

     * get   window dpi

     *

     * @param   res resource

     * @return   window dpi

     */

    private static int getDensity(Resources   res) {

        DisplayMetrics   outMetrics = res.getDisplayMetrics();

        return outMetrics.densityDpi;

    }

 

    /**

     * get dp   size

     *

     * @param   pixelSize pixel size

     * @param   res       resource

     * @return   dp size

     */

    public static int getDpSize(int pixelSize, Resources res) {

        return pixelSize * getDensity(res) / 160;

    }

    /**

     * get   random

     *

     * @param   max random number max value

     * @return   random number

     */

    public static int getRanNum(int max) {

        Random   random = new Random();

        return max > 0 ? random.nextInt(max) : random.nextInt();

    }

}

5ba790ca0001b3a702760435.jpg

这是流星的图片,透明背景和白色的线条模拟流星,

这个是自己PS的5ba790ca00011e6e00400040.jpg


最后在多嘴一句,如果有大神看到还望指点一下

原文链接:http://www.apkbus.com/blog-882499-62900.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消