【问题标题】:Adding imageView over another imageView cause resizing of the bottom one.How to prevent?在另一个图像视图上添加 imageView 会导致底部的调整大小。如何防止?
【发布时间】:2012-05-20 01:07:48
【问题描述】:

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

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

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

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

附:更新问题。

> <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 中取出一张卡片并使用动画将其放置在卡片组 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)}

所有的drawables都是相等的(宽度和高度),所以我希望不会有任何问题,但是当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);

【问题讨论】:

    标签: android layout imageview


    【解决方案1】:

    你能展示你正在使用的代码吗? 无论如何,我认为最好使用 FrameLayout 来显示可能相互重叠的视图。我没有看到代码的第一个想法是用 FrameLayout 包装现有的 ImageView。然后,当您想以编程方式添加新的 ImageView 时,可以通过将其添加为 FrameLayout 的子项来完成。 FrameLayout 在不同的层渲染视图,所以当你对新添加的视图做一些事情时,旧的不应该受到影响。

    [更新]

    我只是试图实现将 imageview 放在另一个 imageview 上。无论如何,我似乎没有得到重叠图像大小的问题。这是我的实现。请试试这个,看看它是否有效。不过,您可能需要更改一些变量。

    .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>
    

    【讨论】:

    • 谢谢Warut,我稍微更新了问题,还尝试发布一些代码和xml。你确定RelativeLayout在添加新ImageViews(不同层)时的行为不像FrameLayout吗?我真的希望有解决方案,因为我真的需要这个RelativeLayout.....
    • 好吧,我不太确定,我只是猜测 FrameLayout 可能会有所帮助。需要立即尝试以证明这一点。我试一试后马上回来; )(我应该还有 4 小时)。
    • 请试试上面的代码。我不确定您的实施情况如何。如果它不起作用,请通知我,以便我可以为您提供更多帮助。
    • Warut,使用您的代码并通过我身边的一些调查,我发现我的问题出在哪里 - 首先我必须承认我对两个可绘制对象的宽度和高度是错误的 - 一个波纹管与我需要放在上面的那个相比,宽度是 +10 像素,高度是 + 10 像素。所以,当我从下面的一个获取LayoutParams 然后将它们设置为上面的那个时 - 两个 imageViews 使用一个相同的布局参数(那些对于上面的卡片) - 这会导致重新绘制下面的图像以适应上面的图像 - 新图像需要“隐藏”下面的整个图像......
    • 如果你尝试添加上面的 ImageView 而不指定 LayoutParam 会怎样?
    【解决方案2】:

    我遇到了同样的问题,我通过缩放另一张图片解决了这个问题,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;
           }
    

    【讨论】:

    • 感谢您的回答,看看问题的更新 - 我希望我错过了一些其他的小东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2019-03-14
    • 2018-10-11
    相关资源
    最近更新 更多