package com.jereh.musicapplication.threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
/**
* Created by zhangdi on 2016/9/1.
* 这是一个线程池的工具类,在用到线程的时候可以直接类名加方法名使用
*/
public class ThreadPoolManager {
/** 线程执行器 **/
private static ExecutorService executorService = null;
/** 固定5个线程 **/
private static int nThreads = 5;
/** 单例 **/
private static ThreadPoolManager taskExecutorPool = null;
/** 初始化线程池 **/
static {
taskExecutorPool = new ThreadPoolManager(nThreads * getNumCores());
}
/** 构造函数 **/
private ThreadPoolManager(int threads) {
//executorService = Executors.newFixedThreadPool(threads);
executorService = Executors.newScheduledThreadPool(threads);
}
/**
* 取得单例
*
* @return
*/
public static ThreadPoolManager getInstance() {
return taskExecutorPool;
}
/**
* 取得线程执行器
*
* @return
*/
public ExecutorService getExecutorService() {
return executorService;
}
/**
* 取得周期性线程执行器
* @return
*/
public ScheduledExecutorService getScheduledExcutorService(){
return (ScheduledExecutorService)executorService;
}
/**
* 获得手机cup个数
* @return
*/
public static int getNumCores() {
int threadCount = Runtime.getRuntime().availableProcessors();
return threadCount;
}
}
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fl"/>
package com.jereh.musicapplication;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import com.jereh.musicapplication.threadpool.ThreadPoolManager;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class FrameActivity extends AppCompatActivity {
private Timer timer;
FrameLayout frameLayout;
Drawable drawable;
android.os.Handler handler = new android.os.Handler(){
int i = 0;
@Override
public void handleMessage(Message msg) {
if (msg.what==1){
i++;
move(i%4);
}
super.handleMessage(msg);
}
};
void move(int i){
drawable = getResources().getDrawable(R.mipmap.ic_launcher,null);
Drawable drawable1 = getResources().getDrawable(R.mipmap.dd1,null);
Drawable drawable2 = getResources().getDrawable(R.mipmap.dd2,null);
Drawable drawable3 = getResources().getDrawable(R.mipmap.dd3,null);
switch (i){
case 0:
frameLayout.setForeground(drawable);
break;
case 1:
frameLayout.setForeground(drawable1);
break;
case 2:
frameLayout.setForeground(drawable2);
break;
case 3:
frameLayout.setForeground(drawable3);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame);
frameLayout = (FrameLayout)findViewById(R.id.fl);
timer = new Timer();
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// handler.sendEmptyMessage(1);
// }
// },0,500);//第二个参数是隔多少秒之后开始显示,第三个是隔多久显示下一个
ThreadPoolManager
.getInstance()
.getScheduledExcutorService()
.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
},0,500, TimeUnit.MILLISECONDS);//第二个参数是隔多少秒之后开始显示,第三个是隔多久显示下一个
}
@Override
protected void onDestroy() {
timer.cancel();
super.onDestroy();
}
}
/**
* 透明效果
* @return
*/
public Animation getAlphaAnimationIn() {
//实例化 AlphaAnimation 主要是改变透明度
//透明度 从 1-不透明 0-完全透明
Animation animation = new AlphaAnimation(1.0f, 0);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(2000);
return animation;
}
public Animation getAlphaAnimationOut() {
//实例化 AlphaAnimation 主要是改变透明度
//透明度 从 1-不透明 0-完全透明
Animation animation = new AlphaAnimation(0, 1.0f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(2000);
return animation;
}
voiceState1.setAnimation(animationIn);
voiceState1.setAnimation(animationOut);
/**
* 监听动画实现动画间的切换
*/
animationOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
voiceState1.startAnimation(animationIn);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animationIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
voiceState1.startAnimation(animationOut);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有