调整位图大小以适应自定义视图中的全屏

时间:2015-02-18 10:40:27

标签: android image bitmap resize

我正在尝试从照片路径导入图像,这是我从另一个活动中抓取的,我让用户从他们的图库中选择了图像。我希望导入的照片调整大小并在全屏显示自定义视图上显示。我希望用户能够绘制完整的图像。我遇到的问题是,导入图像时,它的大小不会调整大小。我使用方法中的w和h在onsizechanged中使用createscaledbitmap,但它仍然没有正确调整大小。有些照片适合整个屏幕(截图),但是从相机拍摄或下载的其他照片要么侧面出来要么伸出来。我一直在尝试一些东西,但似乎无法让它工作。我希望图像全屏显示,以便用户可以看到完整的照片并在其上任意位置绘制。感谢任何可以提供帮助的人!

public class NewCropView extends View {
private Paint paint = new Paint();
private Path path = new Path();
private String myPath;
private Bitmap myBitmap, scaled;
private Canvas myCanvas;
Paint myBitmapPaint;
private boolean onEraseButton;

public NewCropView(Context context, AttributeSet attrs)
{
    super(context, attrs);
    myPath = WelcomeActivity.myPhotoPath;


    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);

    myBitmapPaint = new Paint(Paint.DITHER_FLAG);

}


/**
 * Method that changes the size when the canvas is first created. It is used so that
 * we can use a bitmap as the canvas and when the size changes it creates the bitmap as the canvas.
 * @param w
 * @param h
 * @param oldw
 * @param oldh
 */
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    Log.d("CutAdd", "This is my width in onSizedChanged: " + w);
    Log.d("CutAdd", "This is my height in onSizedChanged: " + h);

    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inMutable = true;
    myBitmap = BitmapFactory.decodeFile(myPath, o);
    scaled = Bitmap.createScaledBitmap(myBitmap, w, h, true);
    myCanvas = new Canvas(scaled);
}

/**
 * Method that creates the new canvas with the paint / path so user can draw on the canvas.
 * @param canvas
 */
@Override
public void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    canvas.drawBitmap(scaled, 0, 0, myBitmapPaint);
    canvas.drawPath(path, paint);
}

/**
 * Method for user touch events, for when they are paining / touching on the screen it follows.
 * @param motionEvent
 * @return
 */
@Override
public boolean onTouchEvent(MotionEvent motionEvent)
{
    float touchX = motionEvent.getX();
    float touchY = motionEvent.getY();

    switch(motionEvent.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_UP:
            break;
    }
    invalidate();
    getTrue();
    return true;
}

0 个答案:

没有答案