0

我有以下问题:在我的布局 xml 中,我有一个相对布局,其中放置了一个 ImageView(可从 app / res 文件夹中绘制,已绘制)。

我需要在与初始图像完全相同的位置以编程方式添加另一个 ImageView(以及来自应用程序资源的另一个可绘制对象)并在其上执行动画。

我几乎成功了,使用新的可绘制对象和从初始 ImageView 获取的布局参数添加新的 ImageView,并在其上执行动画,但初始的 ImageView(在新添加的下方)变得越来越小 - 无缘无故地调整自身大小(其他孩子都可以)。

如何防止这种情况?两个图像的宽度和高度完全相同,我需要保持不变......提前致谢。

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>

现在我需要从deck1 中取出一张卡片并使用动画将它放在deck2 上。所以我执行以下操作:

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)}

所有的可绘制对象都是相等的(宽度和高度),所以我希望不会有任何问题,但是当将动画视图添加到布局时 - 甲板1变小,调整自身大小,它仍然可以点击并显示但更小(并且因为它变小了所有其他依赖它的孩子,所以改变他们的位置......)

对不起,我没有使用真正的代码和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

Can you show the code you are using? Anyway, i think it is better to use FrameLayout to show views that may overlapped to each other. My first thought without seeing the code is to wrap the existing ImageView with FrameLayout. Then when you want to programmatically add new ImageView, you can do it by adding it as FrameLayout's child. FrameLayout renders view in different layer, so the old one should not be affected when you do something with the newly added view.

[updated]

I just tried to implement placing imageview over another imageview. Anyway, I don't seem to get the problem on the size of the overlapped image. Here are my implementation. Please try this and see if it worked or not. You may have to change some variables though.

.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

我遇到了同样的问题,我通过缩放另一张图片解决了这个问题,这将对你有所帮助,告诉我你是否在这段代码中发现了任何好处:)

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 回答