0

次の問題があります。私のレイアウト xml には、ImageView が配置されている相対的なレイアウトがあります (app /res フォルダーから描画可能で、描画されます)。

最初の場所とまったく同じ場所にプログラムで別の ImageView (アプリ リソースから別のドローアブルを使用) を追加し、その上でアニメーションを実行する必要があります。

私はほぼ成功し、新しいドローアブルと初期ImageViewから取得したレイアウトパラメータを使用して新しいImageViewを追加し、その上でアニメーションを実行しますが、初期ImageView(新しく追加されたものの下にあります)は小さくなっています-理由なしにサイズを変更します(他の子はOKです)。

これを防ぐ方法は?両方の画像の幅と高さがまったく同じで、同じままにする必要があります...よろしくお願いします。

PS 質問を更新します。

> <RelativeLayout> .... ... ... <ImageView deck1 > centerverticaly and
> leftof(other UI component) - this is the initial imageview and
> represents deck full of cards.... ... <ImageView deck2>
> centerverticaly and rightof(other UI component) - this is the second
> deck where the cards should be opened.... ... </RelativeLayout>

今度は、デッキ 1 から 1 枚のカードを取り出し、アニメーションを使用してデッキ 2 に配置する必要があります。だから私は次のことをします:

if(animatingView==null){
ImageView animatingview = new ImageView(this.context);
animatingView.setImageBitmap(R.drawable.backofthecard);
animatinView.setLayoutParams(deck1.getLayoutParams);
RelativeLayout.add(animatingView);
}else{
animatingView.setVisible(visible);
}
animatingView.startAnimation(deckTranslateAnimation);

.
.
..
OnAnimationEnd{animatingView.setVisible(invisible)}

すべてのドローアブルは等しい(幅と高さ)ので、問題はないと思いますが、animatingView がレイアウトに追加されると、deck1 が小さくなり、それ自体のサイズが変更され、クリック可能で表示されますが、小さくなります。 (そして、それに依存する他のすべての子が小さくなるため、それらの場所を変更します...)

申し訳ありませんが、実際のコードと xml を使用していませんが、今はそれらの前にいません...

編集:解決済み。コードは次のとおりです。

if(animatingView==null){
                animatingView = new ImageView(context);
                animatingView.setImageResource(R.drawable.back);
                RelativeLayout.LayoutParams params = new LayoutParams(animatingView.getDrawable().getMinimumWidth(), animatingView.getDrawable().getMinimumHeight());
                params.addRule(RelativeLayout.ALIGN_LEFT, this.getDeckView().getCardView().getId());
                params.addRule(RelativeLayout.ALIGN_TOP, this.getDeckView().getCardView().getId());
                animatingView.setLayoutParams(params);
4

2 に答える 2

2

使用しているコードを表示できますか? とにかく、互いに重なる可能性のあるビューを表示するには、FrameLayout を使用する方がよいと思います。コードを見ずに最初に考えたのは、既存の ImageView を FrameLayout でラップすることです。次に、プログラムで新しい ImageView を追加する場合は、FrameLayout の子として追加することで実行できます。FrameLayout は別のレイヤーでビューをレンダリングするため、新しく追加されたビューで何かを実行しても、古いレイヤーは影響を受けません。

[更新しました]

イメージビューを別のイメージビューの上に配置することを実装しようとしました。とにかく、重なった画像のサイズに問題はないようです。これが私の実装です。これを試して、うまくいくかどうかを確認してください。ただし、いくつかの変数を変更する必要がある場合があります。

.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ImageStackActivity extends Activity {

    Context context;
    RelativeLayout relativeLayout;
    ImageView deck1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        context = this.getApplicationContext();
        deck1 = (ImageView)findViewById(R.id.deck1);
        Button button = (Button)findViewById(R.id.button);
        Button button2 = (Button)findViewById(R.id.button2);
        relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageView img = new ImageView(context);
                img.setBackgroundResource(R.drawable.photo3);
                img.setLayoutParams(deck1.getLayoutParams());
                relativeLayout.addView(img);
                Animation anim = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
                img.startAnimation(anim);
            }
        });
        button2.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
                if(relativeLayout.getChildCount()>2){
                    relativeLayout.removeViewAt(2);
                }
            }
        });
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relative_layout"
    >
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:id="@+id/deck1"
      android:src="@drawable/photo2"
    />
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:id="@+id/deck2"
      android:src="@drawable/photo3"
      android:layout_toRightOf="@id/deck1"
    />
</RelativeLayout>
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/button"
    android:text="add image"
    />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/button2"
    android:text="clear"
    />
</LinearLayout>
于 2012-05-18T08:13:07.433 に答える
0

私は同じ問題を抱えていて、他の画像をスケーリングすることで解決しました.mbyこれはあなたを助けます.このコードで何か良いことがあれば教えてください:)

public Bitmap decodeFile(String path, Integer size){
        File f = new File(path);
     try {
         //Decode image size
         BitmapFactory.Options o = new BitmapFactory.Options();
         o.inJustDecodeBounds = true;
         BitmapFactory.decodeStream(new FileInputStream(f),null,o);

         //The new size we want to scale to
         if(size == 0)
          size = 70;

         //Find the correct scale value. It should be the power of 2.
         int scale=1;
         while(o.outWidth/scale/2>=size && o.outHeight/scale/2>=size)
             scale*=2;

         //Decode with inSampleSize
         BitmapFactory.Options o2 = new BitmapFactory.Options();
         o2.inSampleSize=scale;
         return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
     } catch (FileNotFoundException e) {}
     return null;
       }
于 2012-05-18T08:11:52.997 に答える