自定义容器实现类似Windows屏保功能
生活随笔
收集整理的這篇文章主要介紹了
自定义容器实现类似Windows屏保功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
菜鳥進場,方圓十里,寸草不生
寫得很詳細啦,本來是用RxJava來寫的延時,不過考慮到有點門檻就放棄了
import android.animation.ObjectAnimator; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.ViewGroup; import android.widget.ImageView;import com.xhs.backupforcesmarthome.R;import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers;/*** @author zx* @date 2018/3/29 17:09* description*/ public class BubbleBackgroundView extends ViewGroup {private Context context;private Bitmap bitmap;private ScheduledExecutorService scheduledThreadPool;private Thread myThread;private boolean flag = false;public BubbleBackgroundView(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;//初始化泡泡bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bubble);//初始化線程池scheduledThreadPool = Executors.newScheduledThreadPool(1);//初始化線程myThread = new MyThread();}/*** 設置父控件寬高* @param widthMeasureSpec* @param heightMeasureSpec*/@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}/*** 設置子控件位置* @param b* @param i* @param i1* @param i2* @param i3*/@Overrideprotected void onLayout(boolean b, int i, int i1, int i2, int i3) {scheduledThreadPool.schedule(myThread, 2000, TimeUnit.MILLISECONDS);}/*** 線程回調到主線程*/private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (msg.what == 1) {if (getChildCount() >= 20) {removeView(getChildAt(0));}if (getChildCount() < 20) {ImageView imageView = new ImageView(context);imageView.setImageBitmap(bitmap);imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);addView(imageView);setChildLayout(imageView);}}}};/*** 子線程,在線程中進行泡泡生成與添加*/private class MyThread extends Thread{@Overridepublic void run() {super.run();handler.sendEmptyMessage(1);}}/*** 設置子控件的位置* @param imageView 子控件*/private void setChildLayout(ImageView imageView) {//隨機距離左面的距離,控制從控件上面出來還是下面出來int left = new Random().nextInt(getWidth() - bitmap.getWidth()) + bitmap.getWidth();//隨機距離頂部的距離,控制從左面和右面出來int top = new Random().nextInt(getHeight() - bitmap.getHeight()) + bitmap.getHeight();//隨機四個方位int direction = new Random().nextInt(4);//隨機泡泡x、y軸的運動時間long a = new Random().nextInt(8000) + 15000;long b1 = new Random().nextInt(8000) + 15000;//隨機參數控制是往左飄還是往右飄int c = new Random().nextInt(2);//隨機參數控制是往上飄還是往下飄int f = new Random().nextInt(2);int d = 1;int e = 1;if (c == 0) {d = -1;}if (f == 0) {e = -1;}//方位設置switch (direction) {case 0://從上面出來的泡泡imageView.layout(left, 0, left + bitmap.getWidth(), bitmap.getHeight());//控制在y軸上只能往下飄e = 1;break;case 1://從下面出來的泡泡imageView.layout(left, getHeight() - bitmap.getHeight(), left + bitmap.getWidth(), getHeight());//控制在y軸上只能往上飄e = -1;break;case 2://從左面出來的泡泡imageView.layout(0, top, bitmap.getWidth(), bitmap.getHeight() + top);//控制在x軸上只能往右飄d = 1;break;case 3://從右面出來的泡泡imageView.layout(getWidth() - bitmap.getWidth(), top, getWidth(), bitmap.getHeight() + top);//控制在x軸上只能往左飄d = -1;break;default:}//設置動畫ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", 0, d * (getWidth() + bitmap.getWidth()));animator.setDuration(a);ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationY", 0, e * (getHeight() + bitmap.getHeight()));animator1.setDuration(b1);animator.start();animator1.start();}}總結
以上是生活随笔為你收集整理的自定义容器实现类似Windows屏保功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日常部署之OA办公系统源码OA协同办公源
- 下一篇: 2020年IT运维市场大前景到底怎么样