位图imageview在android中的触摸事件上自动缩放

时间:2012-09-10 05:53:31

标签: android bitmap

我正在开发一个应用程序,我想在我的布局上拖动触摸事件上的图像视图。 但问题是我正在从Facebook检索位图图像,而拖动它会自动缩放到较小的图像。

ImageView img = (ImageView)findViewById(R.id.edit_text_image);

Bitmap bm=getFacebookImage();
img.setImageBitmap(bm);

img.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        //Toast.makeText(MoveImage.this,"hiii", Toast.LENGTH_SHORT).show();
        switch(event.getAction())
        {
//      case MotionEvent.ACTION_DOWN:   
//
//      break;

        case MotionEvent.ACTION_MOVE:
            //Toast.makeText(MoveImage.this,"moved", Toast.LENGTH_SHORT).show();
            ImageView img=(ImageView)findViewById(v.getId());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(img.getLayoutParams());
//                 
            int x_cord = (int)event.getRawX();
            int y_cord = (int)event.getRawY();

            if(x_cord>windowwidth){x_cord=windowwidth;}
            if(y_cord>windowheight){y_cord=windowheight;}

            params.leftMargin = x_cord - 25; //(int)event.getX();//
            params.topMargin =  y_cord - 75; // (int)event.getY();//

            img.setLayoutParams(params);

        break;

        }
        return true;
    }
});

如果我从资源中获取图像,它工作正常:

ImageView img = (ImageView)findViewById(R.id.edit_text_image);
img.setBackgroundResource(R.drawable.ic_launcher);

// same touch event here

1 个答案:

答案 0 :(得分:0)

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    StickerRelativeLayout = (RelativeLayout) findViewById(R.id.shsticker_relativeLayout1);
    RelativeLayout.LayoutParams layoutParams;
    layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final ImageView view = new ImageView(getApplicationContext());
    view.setPadding(10, 10, 10, 10);
    view.setImageResource(shstickerBaseAdaptor.shstickerImage[arg2]);
    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            windowwidth = getWindowManager().getDefaultDisplay().getWidth();
            windowheight = getWindowManager().getDefaultDisplay().getHeight();
            android.widget.RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();

            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    Toast.makeText(ImageEditingClass.this, "hii rma jee", Toast.LENGTH_SHORT).show();
                    break;
                case MotionEvent.ACTION_MOVE:

                    RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    int x_cord = (int) event.getRawX();
                    int y_cord = (int) event.getRawY();
                    if (x_cord > windowwidth) {
                        x_cord = windowwidth;
                    }
                    if (y_cord > windowheight) {
                        y_cord = windowheight;
                    }

                    layoutParams.leftMargin = x_cord - 15;
                    layoutParams.topMargin = y_cord -100;
                    layoutParams.rightMargin = x_cord - 50;
                    layoutParams.bottomMargin = x_cord - 50;
                    view.setLayoutParams(layoutParams);
                    break;
                default:
                    break;
            }
            return true;
        }
    });

    StickerRelativeLayout.addView(view, layoutParams);
    Toast.makeText(ImageEditingClass.this, "you click" + arg2, Toast.LENGTH_SHORT).show();
}