Android DrawView Landscape编辑

时间:2016-01-25 19:56:19

标签: android drawing landscape-portrait

我希望能够编辑照片,允许用户在他们拍摄的照片上添加注释。我对肖像照片很好,但风景照片给我带来了很大的问题。问题是绘图区域最终成为整个布局,我只想让照片边界可编辑(可绘制)。我该怎么做呢?肖像很好,因为它填满了布局。

这是它的样子:

Dont want to allow user to edit outside bounds of photo displayed

这是代码XML def:

<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:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <!-- Top Buttons -->

    <LinearLayout
        android:id="@+id/drawToolbar"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/new_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/new_pic" />

        <!--  
        <ImageButton
            android:id="@+id/existing_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/new_pic" />
            -->

        <ImageButton
            android:id="@+id/draw_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/brush" />

        <ImageButton
            android:id="@+id/erase_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/eraser" />

    </LinearLayout>

    <!-- Custom View -->

     <RelativeLayout 
        android:id="@+id/imgStack"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="0dp">

        <ImageView 
            android:id="@+id/imgBackground"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="3dp"
            android:layout_height="fill_parent" 
            android:layout_width="fill_parent"/>

        <com.invocore.fastfield.views.DrawingView
            android:id="@+id/drawing"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="3dp"
            android:background="@android:color/transparent" />

    </RelativeLayout>

    <!-- Color Palette -->

    ... removed buttons to save space...


</LinearLayout>

活动代码如下所示(删除了一些内容以节省空间):

public class PhotoCaptureEditActivity extends Activity implements OnClickListener  {

        //custom drawing view
        private DrawingView drawView;
        private ImageView imgBackground;
        private RelativeLayout imgStack;

        // toolbars
        private LinearLayout drawToolbar, drawColors;

        //buttons
        private ImageButton currPaint, drawBtn, eraseBtn, newBtn;  //, saveBtn, existingBtn;
        //sizes
        private float smallBrush, mediumBrush, largeBrush;


        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_photo_capture_edit);

            ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);

            // reset all pending values
            GlobalState.getInstance().pendingFieldKey = null;
            GlobalState.getInstance().pendingFieldValue = null;

            //get drawing view
            drawView = (DrawingView)findViewById(R.id.drawing);
            drawView.setVisibility(View.VISIBLE); 

            imgBackground = (ImageView)findViewById(R.id.imgBackground);
            imgStack = (RelativeLayout)findViewById(R.id.imgStack);

            // toolbars
            drawToolbar = (LinearLayout)findViewById(R.id.drawToolbar);
            drawColors = (LinearLayout)findViewById(R.id.drawColors);

            //get the palette and first color button
            LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
            currPaint = (ImageButton)paintLayout.getChildAt(0);
            currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));

            [removed button and other code to save space]


            enableDrawing();

            // Must be a PNG for editing
            Bitmap image = Utilities.loadImageByPath(filename);

            if (image != null) {
                imgBackground.setImageBitmap(image);
            }
        }

    //user clicked paint
    public void paintClicked(View view){
        //use chosen color
        setModeText("Draw Mode");

        //set erase false
        drawView.setErase(false);
        drawView.setBrushSize(drawView.getLastBrushSize());

        if(view!=currPaint){
            ImageButton imgView = (ImageButton)view;
            String color = view.getTag().toString();
            drawView.setColor(color);
            //update ui
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
            currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
            currPaint=(ImageButton)view;
        }
    }

    [Lots of other code...]



}

和DrawingView类:

   public class DrawingView extends View {

        //drawing path
        private Path drawPath;
        //drawing and canvas paint
        private Paint drawPaint, canvasPaint;
        //initial color
        private int paintColor = 0xFF660000;
        //canvas
        private Canvas drawCanvas;
        //canvas bitmap
        private Bitmap canvasBitmap;
        //brush sizes
        private float brushSize, lastBrushSize;
        //erase flag
        private boolean erase=false;

        public DrawingView(Context context, AttributeSet attrs){
            super(context, attrs);
            setupDrawing();
        }

        //setup drawing
        private void setupDrawing(){

            //prepare for drawing and setup paint stroke properties
            brushSize = getResources().getInteger(mycompany.R.integer.medium_size);
            lastBrushSize = brushSize;
            drawPath = new Path();
            drawPaint = new Paint();
            drawPaint.setColor(paintColor);
            drawPaint.setAntiAlias(true);
            drawPaint.setStrokeWidth(brushSize);
            drawPaint.setStyle(Paint.Style.STROKE);
            drawPaint.setStrokeJoin(Paint.Join.ROUND);
            drawPaint.setStrokeCap(Paint.Cap.ROUND);
            canvasPaint = new Paint(Paint.DITHER_FLAG);
        }

        //size assigned to view
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            if(canvasBitmap == null) {
                canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
            }
            drawCanvas = new Canvas(canvasBitmap);
            drawCanvas.drawColor(Color.TRANSPARENT);
        }

        //draw the view - will be called after touch event
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
            canvas.drawPath(drawPath, drawPaint);
        }

        //register user touches as drawing action
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float touchX = event.getX();
            float touchY = event.getY();
            //respond to down, move and up events
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                if(erase == true) {
                    drawPath.lineTo(touchX, touchY);
                    drawCanvas.drawPath(drawPath, drawPaint);
                    drawPath.reset();
                    drawPath.moveTo(touchX, touchY); 
                }
                else {
                    drawPath.lineTo(touchX, touchY);
                }
                break;
            case MotionEvent.ACTION_UP:
                drawPath.lineTo(touchX, touchY);
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();
                break;
            default:
                return false;
            }
            //redraw
            invalidate();
            return true;

        }

        //update color
        public void setColor(String newColor){
            invalidate();
            paintColor = Color.parseColor(newColor);
            drawPaint.setColor(paintColor);
            // set into draw mode if color was clicked
            setBrushSize(getLastBrushSize());
            setErase(false);
        }

        //set brush size
        public void setBrushSize(float newSize){
            float pixelAmount = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
                    newSize, getResources().getDisplayMetrics());
            brushSize=pixelAmount;
            drawPaint.setStrokeWidth(brushSize);
        }

        //get and set last brush size
        public void setLastBrushSize(float lastSize){
            lastBrushSize=lastSize;
        }

        public float getLastBrushSize(){
            return lastBrushSize;
        }

        //set erase true or false
        public void setErase(boolean isErase){
            erase=isErase;
            if(erase) drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            else drawPaint.setXfermode(null);
        }

        public void startExisting(String path) {
            File file = new File(path);
            if(file.exists() == true) {
                Bitmap captured = BitmapFactory.decodeFile(file.toString());
                canvasBitmap = captured.copy(Bitmap.Config.ARGB_8888, true);
                drawCanvas = new Canvas(canvasBitmap);
                drawCanvas.drawColor(Color.TRANSPARENT);
                invalidate();
            }
        }

        public void startNew() {
            if(drawCanvas != null) {
                drawCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                invalidate();
            }
            // setting a solid background color
            //drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
            //drawCanvas.drawColor(Color.GREEN, PorterDuff.Mode.CLEAR);
        }

        public Bitmap getImage() {
            return canvasBitmap;
        }

    }

谢谢!

1 个答案:

答案 0 :(得分:0)

            Uri currImageURI = intent.getData();
            String s= getRealPathFromURI(currImageURI);
            File file = new File(s);



            if (file.exists()) {
                Drawable drawImg = Drawable.createFromPath(file.getAbsolutePath());
                drawView.setBackground(drawImg);

            }
            else {
                // file does not exist

            }