动态视图组构造中出现意外剪切

时间:2014-10-14 19:34:17

标签: android viewgroup layoutparams

我构建了一个水平滚动的条带,之前捕获的图像,我希望这个条带的项目看起来像 enter image description here

但我目前只看到 enter image description here

构造过程在功能中实现:

FrameLayout getImageContainer(){
        //Call custom function from application class to get screen size
        DeviceScreenSize dss = app.getScreenSize();

        //Calculate thumbnail size based on 15% size of the minimum of screen sizes
        //imgWidth and imgHeight are globals to use for thumbnail sizes
        imgWidth = imgHeight = Math.min(dss.getWidth(), dss.getHeight())*15/100;

        //Create container for thumbnail(ImageView) and erase button
        FrameLayout fl = new FrameLayout(this);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        fl.setLayoutParams(params);

        //ImageView containing thumbnail of captured image (has 15% size from min screen side)
        ImageView imv = new ImageView(this);
        imv.setId(R.id.imvFrame);
        imv.setLayoutParams( new LayoutParams( imgWidth, imgHeight ) );
        imv.setScaleType(ScaleType.CENTER_CROP);

        //Button to erase captured image from strip without removing from device (has 7% size from min screen side)
        Button imgClose = new Button(this);
        imgClose.setId(R.id.btnErase);
        imgClose.setBackgroundResource(R.drawable.ic_closebutton);
        params = new LayoutParams(
                        Math.min(dss.getWidth(), dss.getHeight())*7/100, 
                        Math.min(dss.getWidth(), dss.getHeight())*7/100);

        //Snap the center point of imgClose button to the right-top corner of ImageView
        params.topMargin = -Math.min(dss.getWidth(), dss.getHeight())*7/200;
        params.leftMargin = imgWidth-Math.min(dss.getWidth(), dss.getHeight())*7/200;

        imgClose.setLayoutParams( params );

        //Add all children to FrameLayout
        fl.addView(imv);
        fl.addView(imgClose);

        //Add to FrameLayout the additional right-side child as whitespace between different images
        View v = new View(this);
        params = new LayoutParams(Math.min(dss.getWidth(), dss.getHeight())*5/200, imgWidth);
        params.leftMargin = imgWidth+Math.min(dss.getWidth(), dss.getHeight())*7/200;
        v.setLayoutParams(params);
        fl.addView(v);

        return fl;
    }

每次捕获图像后都会调用下一个函数:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            switch (requestCode) {
            case CAMERA_IMAGE_REQUEST: {

                //Get image container to place the thumbnail of captured image
                FrameLayout container = getImageContainer();
                ((ImageView)container.findViewById(R.id.imvFrame)).setTag(imageFolderPath + File.separator + imageName);
                ((ImageView)container.findViewById(R.id.imvFrame)).setOnClickListener(openImage);
                ((Button)container.findViewById(R.id.btnErase)).setOnClickListener(eraseImage);

                .......................
                .......................             
                ((ImageView)container.findViewById(R.id.imvFrame)).setImageBitmap(bitmap);
                imvPhotoFrame.addView(container);
            }
            break;
            .......................
            .......................
            }
        }
}

此处,imvPhotoFrame是一个LinearLayout:

.............
<HorizontalScrollView
    android:id="@+id/svHorizontalScroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal" >

    <LinearLayout
        android:id="@+id/imvPhotoFrame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
    </LinearLayout>
</HorizontalScrollView>
.............

那么,我需要做些什么才能从第一张图片中获取条带?

1 个答案:

答案 0 :(得分:1)

我觉得自己真的很蠢,但问我这个问题的解决方案非常简单。 该点位于topMargine属性中,因此上面代码的正确代码段为:

ImageView imv = new ImageView(this);
imv.setId(R.id.imvFrame);
params = new LayoutParams(imgWidth, imgHeight);
imv.setLayoutParams(params);

//set positive top margine for ImageView insteed negative one for button imgClose
params.topMargin = Math.min(dss.getWidth(), dss.getHeight())*7/200;

imv.setScaleType(ScaleType.CENTER_CROP);


Button imgClose = new Button(this);
imgClose.setId(R.id.btnErase);
imgClose.setBackgroundResource(R.drawable.ic_closebutton);
params = new LayoutParams(
                    Math.min(dss.getWidth(), dss.getHeight())*7/100, 
                    Math.min(dss.getWidth(), dss.getHeight())*7/100);

//params.topMargin = -Math.min(dss.getWidth(), dss.getHeight())*7/200;
params.leftMargin = imgWidth-Math.min(dss.getWidth(), dss.getHeight())*7/200;

imgClose.setLayoutParams( params );