在相对布局中动态添加Imageviews后,图像不会调整大小

时间:2016-07-27 06:52:13

标签: java android android-layout imageview android-imageview

我正在尝试制作照片拼贴应用。在这里,我通过传递用于创建网格的位置数组来动态地添加相对布局中的图像视图。网格已成功创建。但是当我添加图像时,图像不能正确缩放或不适合整个图像视图区域。此外,当我在图像上添加触摸监听器时,图像在imageview区域外移动。请帮助我。谢谢提前这是我的代码

public void drawGrids() {
    Resources res = getResources();
    int Rid = c.id;

    TypedArray ta = res.obtainTypedArray(Rid);
    int n = ta.length();
    String[][] array = new String[n][];
    for (int i = 0; i < n; ++i) {
        int id = ta.getResourceId(i, 0);
        if (id > 0) {
            array[i] = res.getStringArray(id);
            Log.e(" array", "" + i + " " + Arrays.toString(array[i]));
            String[] values = Arrays.toString(array[i]).replaceAll("[\\[\\]\\s]", "").split(","); // extracting each element from array

            final int position = i;

            limit = position+1;
            float x = Float.parseFloat(values[0]);
            float y = Float.parseFloat(values[1]);
            float w = Float.parseFloat(values[2]);
            float h = Float.parseFloat(values[3]);
            Log.e(" x:", "" + x);
            Log.e(" y:", "" + y);
            Log.e(" w:", "" + w);
            Log.e(" h:", "" + h);

            img1 = new ImageView(getActivity());

            img1.setImageResource(R.drawable.button_background);         

            params = new RelativeLayout.LayoutParams((int) ((Screen_width * w) - padding), (int) ((Screen_height * h) - padding));

            // x= x* Screen_width
            // y= y* Screen_height
            params.leftMargin = (int) ((Screen_width * x) + padding);
            params.topMargin = (int) ((Screen_height * y) + padding);
            params.rightMargin = padding;
            params.bottomMargin = padding;
            Log.e(" px(x):", "" + (int) (Screen_width * x));
            Log.e(" px(y):", "" + (int) (Screen_height * y));
            Log.e(" px(w):", "" + (int) (Screen_width * w));
            Log.e("px(h)", "" + (int) (Screen_height * h));

            if(!mSelectedImages.isEmpty()) {
                onPickedSuccessfully(mSelectedImages);
            }
            else {
                img1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("clicked", "" + position);
                        onClickPickImageMultipleWithLimit(img1);
                    }
                });
            }

        IMGS.add(img1);  // arraylist of imageview
            root1.addView(img1, params);
            createPreview();
        } else {
            // something wrong with the XML
        }
    }

    ta.recycle(); 


}

 public void onPickedSuccessfully(ArrayList<ImageEntry> images) { //     selected images path are to be fetched here
      mSelectedImages = images;


        handler.post(new Runnable() {
            @Override
            public void run() {
                for (j = 0; j <IMGS.size(); j++) {

                    final ImageView child=IMGS.get(j);

                    child.onTouchListener(new MultiTouchListener);// multitouch listener for zooming and scrolling picked image 
                    Log.e("w n h", "" + child.getWidth() + " " + child.getHeight());

                    int areaheight = child.getWidth();
                    int areawidth = child.getHeight();

                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    oldBitmap = BitmapFactory.decodeFile(String.valueOf(mSelectedImages.get(j)), bmOptions);//decodeFile(String.valueOf(mSelectedImages.get(j)));

                    int height = oldBitmap.getHeight(), width = oldBitmap.getWidth();
                    Log.e(" b width and height ", "" + oldBitmap.getWidth() + " " + oldBitmap.getHeight());
                    Log.e(" area width and height ", "" + areawidth + " " + areaheight);

                    Bitmap scaledBitmap;
                    if (areaheight > areawidth) {
                        // portrait
                        float ratio = (float) height / areaheight;
                        height = areaheight;
                        width = (int) (width / ratio);
                        scaledBitmap = Bitmap.createScaledBitmap(oldBitmap, width, height, true);
                        Log.e("porait scaled w ht ", "" + scaledBitmap.getWidth() + " " + scaledBitmap.getHeight());

                    } else if (areawidth > areaheight) {
                        //landscape
                        float ratio = (float) width / areawidth;
                        width = areawidth;
                        height = (int) (height / ratio);
                        scaledBitmap = Bitmap.createScaledBitmap(oldBitmap, width, height, true);
                        Log.e("landscape scaled w ht ", "" + scaledBitmap.getWidth() + " " + scaledBitmap.getHeight());
                    } else {
                        // square
                        height = areaheight;
                        width = areawidth;
                        scaledBitmap = Bitmap.createScaledBitmap(oldBitmap, width, height, true);
                        Log.e("square scaled w ht ", "" + scaledBitmap.getWidth() + " " + scaledBitmap.getHeight());

                    }
                    child.setImageBitmap(scaledBitmap);
                }
            }
        });

2 个答案:

答案 0 :(得分:1)

在您的情况下,您需要添加行:

img1.setScaleType(ImageView.ScaleType.CENTER_CROP); // or CENTER_INSIDE, or FIT_CENTER, or for example FIT_XY

后:

img1.setImageResource(R.drawable.button_background);

因此,您将为将要设置到ImageView中的所有图像提供缩放。您可以阅读有关scaleType herehere

的更多信息

答案 1 :(得分:1)

您可以尝试使用此自定义视图,而不是使用ImageView和setOnTouchListener。

function showAlert(msg) {//Messagebox with out grid page

var showAlerObject = {};
showAlerObject.$dvMessageBox = $("#dvMessageBox");
showAlerObject.$dialogboxForMessage = $('#dialogboxForMessage');
showAlerObject.$dvMessageBox.html(msg);
$(".ui-widget").css({ "font-weight": +"bold" });
showAlerObject.$dialogboxForMessage.dialog({
    title: 'Edu Plus',
    showOnLoad: false,
    autoExpand: false,
    autoOpen: false,
    width: 370,
    modal: true,
    close: function () {

        showAlerObject.$dialogboxForMessage.dialog('destroy');
        showAlerObject.$dvMessageBox.html('');
    },
    buttons: {

        OK: function () {
            if (showAlerObject.okClick !== null) {
                showAlerObject.okClick();
            }
            showAlerObject.$dialogboxForMessage.dialog('destroy');
            showAlerObject.$dvMessageBox.html('');
        }
    },
    show: 'fade',
    hide: 'fade',
    dialogClass: 'main-dialog-class'
});
showAlerObject.$dialogboxForMessage.dialog("open"); 
return showAlerObject;

限制视图内至少有一个角落的图像:

public class ScaleImageView extends ImageView {

private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.0f;
private float x = 0;
private float y = 0;
private float tx = 0;
private float ty = 0;
private float dx = 0;
private float dy = 0;
private int scrollLimitX = 0;
private int scrollLimitY = 0;
private boolean justScaled = false;

public ScaleImageView(Context context) {
    super(context);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

public ScaleImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if(ev.getPointerCount() == 1){
        switch(ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            tx = ev.getX();
            ty = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            if(!justScaled){
                dx = tx - ev.getX();
                dy = ty - ev.getY();
                tx -= dx;
                ty -= dy;
                int scrollX = (int)(this.getScrollX()/mScaleFactor);
                int scrollY = (int)(this.getScrollY()/mScaleFactor);
                if(Math.abs(scrollX+dx) > scrollLimitX) dx = 0;
                if(Math.abs(scrollY+dy) > scrollLimitY) dy = 0;
                this.scrollBy((int)(dx*mScaleFactor), (int)(dy*mScaleFactor));
            }
            break;
        case MotionEvent.ACTION_UP:
            justScaled = false;
            break;
        }
    }else if(ev.getPointerCount() == 2){
        justScaled = true;
        mScaleDetector.onTouchEvent(ev);
    }
    return super.onTouchEvent(ev);
}

@Override
public void onDraw(Canvas canvas) {
    x = this.getWidth()/2;
    y = this.getHeight()/2;
    canvas.scale(mScaleFactor, mScaleFactor, x ,y);
    int scrollX = (int)(this.getScrollX()/mScaleFactor);
    int scrollY = (int)(this.getScrollY()/mScaleFactor);
    if(Math.abs(scrollX) > scrollLimitX) dx = scrollLimitX - scrollX; else dx = 0;
    if(Math.abs(scrollY) > scrollLimitY) dy = scrollLimitY - scrollY; else dy = 0;
    this.scrollBy((int)(dx*mScaleFactor), (int)(dy*mScaleFactor));
    super.onDraw(canvas);
}

public void setScaleFactor(float mfactor){
    this.mScaleFactor = mfactor;
}

public float getScaleFactor(){
    return this.mScaleFactor;
}

public void setScrollLimit(int x, int y){
    this.scrollLimitX = x/2;
    this.scrollLimitY = y/2;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 5.0f));
        ScaleImageView.this.invalidate();
        return true;
    }
}

}

希望这有用!