有一些问题,没有找到解决方案。问题是,我有一个位于布局上的图像视图,而此布局位于窗口管理器上,该图像视图是可单击的。该图像视图必须左右移动并可以单击。这是我的代码
公共类FloatingAnimationService扩展了服务{
private WindowManager windowManager;
private ImageView floatingUnit;
private boolean isClicked;
public void onCreate() {
super.onCreate();
isClicked = false;
floatingUnit = new ImageView(this);
//a unit as imageView
Picasso.with(this).load(new File(Environment.getExternalStorageDirectory().getPath() +"/" + "cat.png")).into(floatingUnit);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(AppMethods.getPxFromDp(120 ,this), AppMethods.getPxFromDp(120 , this));
floatingUnit.setLayoutParams(layoutParams);
final LinearLayout floatingUnitLayout = new LinearLayout(this);
floatingUnitLayout.addView(floatingUnit);
windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
//here is all the science of params
final LayoutParams myParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myParams.gravity = Gravity.TOP | Gravity.LEFT;
myParams.x=0;
myParams.y=100;
windowManager.addView(floatingUnitLayout, myParams);
// add a floatingUnit icon in window
Animation animation = AnimationUtils.loadAnimation(this ,R.anim.floating_unit_animation);
floatingUnit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startQuestion();
stopSelf();
windowManager.removeViewImmediate(floatingUnitLayout);
isClicked = true;
}
});
floatingUnit.startAnimation(animation);
startAnimationTimer(floatingUnitLayout);
AnimationSet animationSet = new AnimationSet(true);
myParams.x = 100;
myParams.y = 100;
windowManager.updateViewLayout(floatingUnitLayout, myParams);
Animation animationToRight = AnimationUtils.loadAnimation(this ,R.anim.left_to_right_animation);
Animation animationToLeft = AnimationUtils.loadAnimation(this , R.anim.right_to_left_animation);
animationSet.addAnimation(animationToRight);
floatingUnit.startAnimation(animation);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private void startQuestion (){
Intent intentQuestionActivity = new Intent(this, QuestionActivity.class);
intentQuestionActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentQuestionActivity);
}
private void startAnimationTimer(final View floatingUnitLayout){
long animationLifeTime = 10000;
new CountDownTimer(animationLifeTime, 1000) {
public void onTick(long millisUntilFinished) {
Log.i("animation remaining: " , Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
Log.i("animation: " , "DONE");
if(!isClicked) {
windowManager.removeViewImmediate(floatingUnitLayout);
stopSelf();
}
}
}.start();
}
`
请帮忙。谢谢 !
最佳答案
谢谢大家。自己找到解决方案。诀窍是:要移动放置在窗口管理器上的图像,必须更新布局参数。
这是下面的代码
private void startAnimationTimer(final View floatingUnitLayout) {
long animationLifeTime = AppData.first(AppData.class).getCountSecondsPicShow();
countDownTimer = new CountDownTimer(animationLifeTime, 100) {
public void onTick(long millisUntilFinished) {
Log.i("animation remaining: ", Long.toString(millisUntilFinished / 1000));
myParams.x = myParams.x + 2;
myParams.y = myParams.y + 2;
windowManager.updateViewLayout(floatingUnitLayout, myParams);
}
public void onFinish() {
Log.i("animation: ", "DONE");
if (!isClicked) {
windowManager.removeViewImmediate(floatingUnitLayout);
stopSelf();
}
}
}.start();
}
关于android - Android:将imageView多数民众赞成通过动画放置在窗口管理器上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39791387/