将图像放大并置于屏幕中央

3

我想要将一个只包含图片的视图放在主布局中心屏幕上方,目前我只能将充气视图添加为主布局的第一个子项。如何将ImageView充气到其他视图之上并位于屏幕中央?

LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.image_preview,(ViewGroup)findViewById(R.id.previewHolder));
        ((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).addView(layout);//add inflated to main view

主布局
<LinearLayout 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"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
 >

<LinearLayout
    android:id="@+id/fullScreen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</LinearLayout>

<TableLayout
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:focusableInTouchMode="true"
    android:stretchColumns="*" >
    ........................
   </TableLayout>
   </LinearLayout>

活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_testing_cam);
            badge.setOnClickListener(btnListener);
          }
  private OnClickListener btnListener = new OnClickListener() { 

         @Override
       public void onClick(View v) {
           //SELECTS BUTTON ACTIONS
        switch(v.getId()){

            case R.id.status:
                finish();
                break;
            default:
                previewImages(v.getId());
                break;
        }
    }
}; 
            //CARRY OUT IMAGE FUNCTIONS
public void previewImages(int index){
    try{

        LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.image_preview,(ViewGroup)findViewById(R.id.previewHolder));
        ((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).addView(layout);



    ImageView iv = (ImageView)findViewById(R.id.previewImage);
    BitmapDrawable drawable = (BitmapDrawable) ((ImageView) showCase.getChildAt(index)).getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    iv.setImageBitmap(bitmap);
    ((LinearLayout) (LinearLayout)findViewById(R.id.fullScreen)).bringToFront();
    }catch(Exception e){ Log.i("ERROR FLATE", e.toString()); }
    return;
}

正在被膨胀的视图

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/previewImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"/>


你想在 fullScreenroot 上方添加这样的视图吗? - a.bertucci
目前我正在将新视图添加到fullscreen中,因为我无法将其添加到顶级父元素中。所以是的,我希望它在所有视图之上,然后当我点击时再将其移除。它应该是一个占据整个屏幕的ImageView。 - kabuto178
我想你正在某个活动中使用这个布局。你把第一段代码放在哪里了? - a.bertucci
onClickListener 中调用它,在单击按钮后,我想要填充与该按钮相关联的图片的全屏预览。 - kabuto178
你能发一下你的Activity代码吗? - a.bertucci
1个回答

1
如果你想在全屏和根视图之上添加预览,你应该修改父布局。LinearLayout会垂直排列其子项,在这种情况下,你需要使用FrameLayout或RelativeLayout。类似这样:
<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"
  android:focusable="true">

  <!-- your previous content -->
  <LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
      android:id="@+id/fullScreen"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
    </LinearLayout>

    <TableLayout
      android:id="@+id/root"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:focusableInTouchMode="true"
      android:stretchColumns="*" >
      ........................
    </TableLayout>

  </LinearLayout>

  <!-- your preview -->
  <ImageView
    android:id="@+id/preview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone"/>

</RelativeLayout>

当单击时,您需要做的就是更改预览ImageView的可见性,并相应地设置其源。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接