2016-07-11 7 views
-2

私は画像の中にRecyclerViewを持っています。クリックした画像を画面の中央に移動する必要があります。そして、画像の開始点に依存してはいけません。画面の中央に移動する必要があります。翻訳アニメーションAndroid

私はこれをXMLコードを使用して実行しようとしました - それは正しく動作しません、TranslateAnimationオブジェクト - 正しく動作しません。両方の変種において、画像の動きは画像の開始位置に依存し、全ての画像の最終点は異なる。

私はこれを行う方法がわかりません。助けてください)

+0

ここまでお試しください。ありがとうございました。 –

答えて

0

次のコードを試してください。コードはListviewを使用していますが、同じロジックをRecyclerViewに適用することができます。

ここでのアプローチは、クリックされたイメージと重なっているリストビューの親レイアウトに新しいイメージビューを作成することです。次に、新しく作成した画像ビューを画面の中央に移動します。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    root = (RelativeLayout) findViewById(R.id.main_root); 
    listView = (ListView) findViewById(R.id.list); 

    MyAdapter adapter = new MyAdapter(MainActivity.this, web, imageId); 

    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      ImageView imgView = (ImageView) view.findViewById(R.id.grid_image); 

      // Get location of window with respect to window. 
      int location[] = new int[2]; 
      imgView.getLocationInWindow(location); 

      // Create a new image view overlapping 
      // the image view that was clicked. 
      ImageView imgView2 = new ImageView(MainActivity.this); 
      imgView2.setImageDrawable(imgView.getDrawable()); 

      // To make it overlap, use the location values of 
      // the clicked image as left and top margin for the 
      // new image. 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        imgView.getWidth(), imgView.getHeight()); 
      params.leftMargin = location[0]; 
      params.topMargin = location[1] - getStatusBarHeight(); 

      // Add the new image view to the root view of the activity. 
      root.addView(imgView2, params); 

      translateToCenter(imgView2, location); 
     } 
    }); 

} 

/** 
* To translate the new image view to the center of the screen. 
* @param view 
* @param originalLoc 
*/ 
private void translateToCenter(View view , int originalLoc[]) 
{ 
    int xMove = root.getWidth()/2 - view.getWidth()/2 - originalLoc[0]; 
    int yMove = root.getHeight()/2 - view.getHeight()/2 - originalLoc[1]; 

    TranslateAnimation anim = new TranslateAnimation(0, xMove , 0, yMove); 
    anim.setDuration(1000); 
    anim.setFillAfter(true); 
    view.startAnimation(anim); 
} 

/** 
* To get the status bar height. 
* @return 
*/ 
private int getStatusBarHeight() { 
    int result = 0; 
    int resourceId = getResources().getIdentifier(
      "status_bar_height", "dimen", "android"); 
    if (resourceId > 0) { 
     result = getResources().getDimensionPixelSize(resourceId); 
    } 
    return result; 
} 
+0

ありがとうございます)) –

関連する問題