my image not set in the center of relative layout and its not showing complete image using flood-fill and canvas

81 Views Asked by At

I want to set my bitmap converted image in relative layout with the help of custom view but my image not set in the center of relative layout and its not showing complete image

My relative layout where I set my bitmap image`package org.boostram.justcolor;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context; 
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.OnColorChangedListener;
import com.flask.colorpicker.OnColorSelectedListener;

import org.boostram.justcolor.Utils.util;  

import static android.R.attr.src;


public class FillPaintActivity extends Activity implements View.OnTouchListener {
private RelativeLayout dashBoard;
Paint paint;
private MyView myView;
int a = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myView = new MyView(this);
    setContentView(R.layout.activity_fill_paint);
    ColorPickerView colorPickerView = (ColorPickerView) findViewById(R.id.color_picker_view);
    //    colorPickerView.setColor(Color.WHITE,true);
    paint.setColor(Color.WHITE);
    colorPickerView.setInitialColor(Color.WHITE, true);
    colorPickerView.addOnColorChangedListener(new OnColorChangedListener() {
        @Override
        public void onColorChanged(int selectedColor) {
            a = 1;
            paint.setColor(selectedColor);
            // Handle on color change
//                Log.d("ColorPicker", "onColorChanged: 0x" + Integer.toHexString(selectedColor));
        }
    });
    colorPickerView.addOnColorSelectedListener(new OnColorSelectedListener() {
        @Override
        public void onColorSelected(int selectedColor) {
            paint.setColor(selectedColor);
            // myView.changePaintColor(selectedColor);
            Toast.makeText(FillPaintActivity.this, "selectedColor: " + 
  Integer.toHexString(selectedColor).toUpperCase(),
//                        Toast.LENGTH_SHORT).show();
        }
    });
    dashBoard = (RelativeLayout) findViewById(R.id.dashBoard);
    dashBoard.addView(myView);


}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return false;
}

public class MyView extends View {

    // private Paint paint;
    private Path path;
    public Bitmap mBitmap;
    public ProgressDialog pd;
    final Point p1 = new Point();
    public Canvas canvas;

    public MyView(Context context) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
        pd = new ProgressDialog(context);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        // paint.setStrokeWidth(5f);
//            BitmapDrawable drawable = (BitmapDrawable) 
 context.getDrawable(R.drawable.unnamed);
//            Bitmap bmp = drawable.getBitmap();
//            Bitmap mBitmap = Bitmap.createScaledBitmap(bmp, 120, 120, 
false);
        mBitmap = BitmapFactory.decodeResource(getResources(), 
  util.Id).copy(Bitmap.Config.ARGB_8888, true);
          //mBitmap = 
Bitmap.createScaledBitmap((BitmapFactory.decodeResource(getResources(), 
util.Id).copy(Bitmap.Config.ARGB_8888, true)),1500,1380,false);
        this.path = new Path();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        this.canvas = canvas;
//            Display display = getWindowManager().getDefaultDisplay();
//            int displayWidth = display.getWidth();
  //            BitmapFactory.Options options = new BitmapFactory.Options();
 //            options.inJustDecodeBounds = true;
 //            BitmapFactory.decodeResource(getResources(), util.Id,  options);
  //            int width = options.outWidth;
 //            if (width > displayWidth)
 //            {
 //                int widthRatio = Math.round((float) width / (float) 
 displayWidth);
  //                options.inSampleSize = widthRatio;
   //            }
  //            options.inJustDecodeBounds = false;
  //            Bitmap scaledBitmap =   
    BitmapFactory.decodeResource(getResources(),util.Id , options);
        canvas.drawBitmap(mBitmap,0,0 ,paint);
    //            mBitmap = BitmapFactory.decodeResource
       }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (a == 0) {
            Toast.makeText(FillPaintActivity.this, "Select Any Color First", 
   Toast.LENGTH_SHORT).show();
        }
        //  Toast.makeText(FillPaintActivity.this, myView.getWidth()+""+myView.getHeight(), Toast.LENGTH_LONG).show();
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                p1.x = (int) x;
                p1.y = (int) y;
                if (p1.x < mBitmap.getWidth() && p1.y < mBitmap.getHeight()) {
                    final int sourceColor = mBitmap.getPixel((int) x, (int) y);
                    final int targetColor = paint.getColor();
                    new TheTask(mBitmap, p1, sourceColor, targetColor).execute();
                    invalidate();
                }
//                    else {
                  //  Toast.makeText(FillPaintActivity.this, "You touched outside the Image", Toast.LENGTH_SHORT).show();
     //           }
        }
      return true;
    }

    public void clear() {
        path.reset();
        invalidate();
    }

    public void changePaintColor(int color) {
        paint.setColor(color);
    }

    class TheTask extends AsyncTask<Void, Integer, Void> {

        Bitmap bmp;
        Point pt;
        int replacementColor, targetColor;

        public TheTask(Bitmap bm, Point p, int sc, int tc) {
            this.bmp = bm;
            this.pt = p;
            this.replacementColor = tc;
            this.targetColor = sc;
        }

        @Override
        protected void onPreExecute() {
            // pd.show();

        }

        @Override
        protected void onProgressUpdate(Integer... values) {

        }

        @Override
        protected Void doInBackground(Void... params) {
            FloodFill floodFill = new FloodFill(bmp, targetColor, replacementColor);
            floodFill.floodFill(pt.x, pt.y);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            invalidate();
        }
    }
   }
}

https://i.stack.imgur.com/AHpcb.png

My bitmap converted image not set in center of relative layout through view. Kindly help me i'm stuck here for almost two week. Help

0

There are 0 best solutions below