1

어떤 문제가있어 해결책을 찾지 못했습니다. 문제는 레이아웃에 배치 된 이미지보기가 있고이 레이아웃이 창 관리자에 있으며이 이미지보기를 클릭 할 수 있다는 것입니다. 이 이미지보기는 오른쪽 및 왼쪽으로 이동해야하며 클릭 할 수 있어야합니다. 여기에 내 코드Android : move imageView 애니메이션이있는 창 관리자에 누워 있습니다.

입니다`공용 클래스 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(); 


} 

` 을 확장합니다. 감사 !

+1

아래의 코드 그러나 문제는 무엇인가? – vovahost

+0

문제는 제가 imageView를 화면 오른쪽으로 옮기고 클릭 가능하게 만들 수 없다는 것입니다. –

+0

지금 어떻게 표시되는지 스크린 샷을 추가하십시오. 또는 모든 코드를 포함 시켜서 스스로 테스트 할 수 있습니다. – vovahost

답변

1

감사합니다. 나 혼자 해결책을 찾았 어. 트릭은 : 창 관리자에 누워있는 이미지를 이동하려면 레이아웃 매개 변수를 업데이트해야합니다. 여기

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(); 


}