调整ImageView的使它改变其位置 [英] resizing the imageView makes it change its place

查看:112
本文介绍了调整ImageView的使它改变其位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些图像的相对布局在像这样下面。

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent>


    < ImageView的
        机器人:ID =@ + ID / IMAGE_2
        机器人:layout_width =30dp
        机器人:layout_height =30dp
        机器人:layout_alignLeft =@ + ID /增长
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =34dp
        机器人:layout_marginTop =34dp
        机器人:SRC =@可绘制/ ic_launcher/>

    < ImageView的
        机器人:ID =@ + ID / IMAGE_1
        机器人:layout_width =30dp
        机器人:layout_height =30dp
        机器人:layout_alignBottom =@ + ID / IMAGE_2
        机器人:layout_marginBottom =14dp
        机器人:layout_toRightOf =@ + ID /增长
        机器人:SRC =@可绘制/ ic_launcher/>

    < ImageView的
        机器人:ID =@ + ID / image_8
        机器人:layout_width =30dp
        机器人:layout_height =30dp
        机器人:layout_alignParentTop =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:SRC =@可绘制/ ic_launcher/>
< / RelativeLayout的>
 

和我的java文件,我需要完成一个小任务。 当我点击图像它的大小应该增加。 我尝试这样做,这是工作,这是我的code

 公共类MainActivity扩展活动实现OnClickListener {

查看ImageView的;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    ImageView的I1 =(ImageView的)findViewById(R.id.image_1);
    ImageView的I2 =(ImageView的)findViewById(R.id.image_2);
             ImageView的I3 =(ImageView的)findViewById(R.id.image_8);
            i1.setOnClickListener(本);
    i2.setOnClickListener(本);
    i3.setOnClickListener(本);
}

公共无效的onClick(查看为arg0){
    Log.v(点击,点击);
    ImageView的=为arg0;
    Toast.makeText(这一点,希望调整图像大小,1).show();
    Toast.makeText(此,点击2,1).show();

    身高= imageView.getHeight();
        宽度= imageView.getWidth();
    身高+ = 50;
    宽+ = 50;
    imageView.setLayoutParams(新RelativeLayout.LayoutParams(高度,宽度));

}

}
 

但我面临的一个问题。 当我点击的形象它的位置正在发生变化。 考虑到这些屏幕截图。

下面是当应用程序启动的屏幕截图

和下面是一个图像视图被点击时的屏幕截图。

任何人都可以给我建议,使图像不改变其位置 我可以使用像使用网格视图(如果可能的话。但即使我试图把一些拖拽选项给它。所以任何人都可以建议我请)

更新时间是code:

的onclick方法更新如下:

 公共无效的onClick(查看为arg0){
    Log.v(点击,点击);
    ImageView的=为arg0;
    Toast.makeText(这一点,希望调整图像大小,1).show();
    Toast.makeText(此,点击2,1).show();

    身高= imageView.getHeight();
宽度= imageView.getWidth();
    身高+ = 50;
    宽+ = 50;
    RelativeLayout.LayoutParams PARAMS =新
          RelativeLayout.LayoutParams(高度,宽度);

    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
    params.setMargins(
                imageView.getLeft() -  30,imageView.getTop() -  30,imageView.getRight()+ 30,
               imageView.getBottom()+ 30);
    imageView.setLayoutParams(PARAMS);



}
 

解决方案

您一个相对布局创造新的LayoutParams,只设置高度和宽度。因此,图像视图会默认父相对布局到左上角。您还需要为需要设置的调整和利润。

由于您使用的是相对布局,你需要创建新的RelativeLayout的的LayoutParams:

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

事情是这样的:

  RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
params.setMargins(左,上,右,下);
 

祝你好运...

I am having a Relative layout with few images in it like this below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


    <ImageView
        android:id="@+id/image_2"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/increase"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="34dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/image_1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignBottom="@+id/image_2"
        android:layout_marginBottom="14dp"
        android:layout_toRightOf="@+id/increase"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/image_8"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

and in my java file i need to perform a small task. When i click an image it's size should be increased. I tried this and it was working and this is my code

public class MainActivity extends Activity implements OnClickListener {

View imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView i1 = (ImageView ) findViewById(R.id.image_1);
    ImageView i2 = (ImageView ) findViewById(R.id.image_2);
             ImageView i3 = (ImageView ) findViewById(R.id.image_8);
            i1.setOnClickListener(this);
    i2.setOnClickListener(this);
    i3.setOnClickListener(this);
}

public void onClick(View arg0) {
    Log.v("clicked ", "clicked");
    imageView = arg0;
    Toast.makeText(this, "looking to resize the image ", 1).show();
    Toast.makeText(this, "clicked 2", 1).show();

    height = imageView.getHeight();
        width = imageView.getWidth();
    height += 50;
    width += 50;
    imageView.setLayoutParams(new RelativeLayout.LayoutParams(height, width));

}   

}

But i am facing a problem. when i click the image its position is changing. Consider these screen shots ..

The below is the screen shot when the application is launched

and below is the screen shot when an image view is clicked.

Can anyone suggest me so that the image doesnt change its position I can use something like using a grid view (if possible. but even i am trying to put some drag option to it. So can anyone suggest me please)

UPDATED CODE :

the onclick method is updated as below :

public void onClick(View arg0) {
    Log.v("clicked ", "clicked");
    imageView = arg0;
    Toast.makeText(this, "looking to resize the image ", 1).show();
    Toast.makeText(this, "clicked 2", 1).show();

    height = imageView.getHeight();
width = imageView.getWidth();
    height += 50;
    width += 50;
    RelativeLayout.LayoutParams params = new
          RelativeLayout.LayoutParams(height, width);

    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    params.setMargins( 
                imageView.getLeft()-30,imageView.getTop()-30,imageView.getRight()+30,
               imageView.getBottom()+30);
    imageView.setLayoutParams(params);



}

解决方案

You create new layoutparams for a relative layout and only set the height and width. Therefore, the image view will default to top left of the parent relative layout. You also need to set the alignment and margin as you need.

Since you are using a relative layout, you need to create new RelativeLayout layoutParams:

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

Something like this:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.setMargins(left, top, right, bottom);

Good luck...

这篇关于调整ImageView的使它改变其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆