绘画时如何将旧的位图显示到DrawingView中

时间:2019-02-10 18:46:28

标签: java android

我要编辑我的绘画涂色图像。我正在获取位图图像并将其设置为画布位图,但是旧图像没有出现。我尝试使用drawingView.refresh()recycle()方法,但无法使其正常工作。

    private DrawingView mDrawingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawing);

        if(datadtlimgsItem !=null && datadtlimgsItem.getImageStr() 
        !=null) {
            decodedString = Base64.decode(datadtlimgsItem.getImageStr(), 
            Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, 
            decodedString.length);
            decodedByte = Util.resize(decodedByte, 400, 100);
            mDrawingView.canvasBitmap = decodedByte;
       }
    }

public class DrawingView extends View{

    // To hold the path that will be drawn.
    private Path drawPath;
    // Paint object to draw drawPath and drawCanvas.
    private Paint drawPaint, canvasPaint;
    // initial color
    private int paintColor = 0xff000000;
    private int previousColor = paintColor;
    // canvas on which drawing takes place.
    private Canvas drawCanvas;
    // canvas bitmap
    public Bitmap canvasBitmap;
    // Brush stroke width
    private float brushSize, lastBrushSize;
    // To enable and disable erasing mode.
    private boolean erase = false;

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

    /**
     * Initialize all objects required for drawing here.
     * One time initialization reduces resource consumption.
     */
    private void setUpDrawing(){
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        // Making drawing smooth.
        drawPaint.setAntiAlias(true);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);

        canvasPaint = new Paint(Paint.DITHER_FLAG);

        // Initial brush size is medium.
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;
        drawPaint.setStrokeWidth(brushSize);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    canvasBitmap = Bitmap.createBitmap(w, h, 
    Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

            canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // X and Y position of user touch.
        float touchX = event.getX();
        float touchY = event.getY();
        // Draw the path according to the touch event taking place.
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                drawPath.lineTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_UP:
                if (erase){
                    drawPaint.setXfermode(new 
    PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                }
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();
                drawPaint.setXfermode(null);
                break;
            default:
                return false;
        }

        // invalidate the view so that canvas is redrawn.
        invalidate();
        return true;
    }

    public void setColor(String newColor){
        // invalidate the view
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);
        previousColor = paintColor;
    }

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

    public void setLastBrushSize(float lastSize){
        lastBrushSize=lastSize;
    }
    public float getLastBrushSize(){
        return lastBrushSize;
    }

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

    public void startNew(){
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }
}

我有一个类似于“绘画”的视图,我想显示当前视图中从活动中获取的旧图像,然后编辑我的图像。

image is blank but i need to show previous image here

here is saved image want to edit this image

我有一个类似于Paint的视图,我想显示当前视图中从活动中获取的旧图像,然后编辑我的图像。我有一个类似“绘画”的视图,我想显示当前视图中从活动中获取的旧图像,然后编辑我的图像。我有一个类似“绘画”的视图,我想显示当前视图中从活动中获取的旧图像,然后编辑我的图像。我有一个类似“绘画”的视图,我想显示当前视图中从活动中获取的旧图像,然后编辑我的图像。我有一个类似于“绘画”的视图,我想显示当前视图中从活动中获取的旧图像,然后编辑我的图像。

1 个答案:

答案 0 :(得分:0)

                class Gallery_adm_adp(
val mylist: ArrayList<Model_class_one>,
val fragmentActivity: FragmentActivity,
val pages: Int
      ) :
      RecyclerView.Adapter<Gallery_adm_adp.Viewholder>() {
      var currentpage = 2
      lateinit var loadmore: RelativeLayout
       lateinit var image: ImageView
         lateinit internal var edit_menu: ImageView


inner class Viewholder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    @SuppressLint("ShowToast")
    fun mydata(list: Model_class_one?, position: Int) {
        edit_menu = itemView.findViewById(R.id.edit_menu)
        Log.i("List_check", "running position>> " + position)
        loadmore.visibility = View.GONE

        Glide.with(fragmentActivity).load(list!!.Image).into(image)

        if (position == mylist.size - 1) {
            if (pages >= currentpage) {
                loadmore.setVisibility(View.VISIBLE)
                callnextapi()
            } else {
                loadmore.setVisibility(View.GONE)
            }
        } else {
            loadmore.setVisibility(View.GONE)
        }

        edit_menu.setOnClickListener {


            val id = list.ID
            popup(itemView, id, position)
            Log.d("clicks", "position" + position)
        }
    }
}

private fun popup(itemView: View, id: String, position: Int) {
    val popup: PopupMenu
    popup = PopupMenu(fragmentActivity, itemView.edit_menu)
    popup.inflate(R.menu.menu_delete)
    popup.setOnMenuItemClickListener { item ->
        when (item.itemId) {
            R.id.delete -> {
                showdialog(id, position)
            }
        }
        false
    }
    //displaying the popup
    popup.show()
}

private fun showdialog(ids: String, position: Int) {
    val builder = AlertDialog.Builder(fragmentActivity)
    builder.setTitle("Delete")
        .setMessage("Do You Want to delete this post")
        .setCancelable(false)
        .setPositiveButton("Yes") { dialog, id ->
            deleteitem(ids, position)
        }.setNegativeButton("No") { dialogInterface, i ->
        }
    val alert = builder.create()
    alert.show()
}

private fun toast(text: String) {
    ((fragmentActivity as MainActivity_Admin).customtoast(text))
    Toast.makeText(fragmentActivity, text, Toast.LENGTH_SHORT).show()
}

private fun deleteitem(id: String, position: Int) {
    val requestQueue = Volley.newRequestQueue(fragmentActivity)
    val URL = Constant.DeleteGallery + id
    val stringRequest = object : StringRequest(Request.Method.POST, URL,
        Response.Listener { response ->
            Log.i("VOLLEY_res", "my responce>>$response")
            try {
                val result = response
                toast(result)
                mylist.removeAt(position)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, mylist.size)

            } catch (e: JSONException) {
                e.printStackTrace()
                toast("Can't Delete .. Please Try Again")
            }
        }, Response.ErrorListener { error ->
            toast("Check Internet Connection and Try Again")
        }) {
    }
    requestQueue.add(stringRequest)

}


override fun getItemViewType(position: Int): Int {
    return position
}

private fun callnextapi() {
    val requestQueue = Volley.newRequestQueue(fragmentActivity)
    val URL = Constant.Gallery + currentpage

    val stringRequest = object : StringRequest(Request.Method.POST, URL,
        Response.Listener { response ->
            try {
                val result = JSONObject(response)
                val ListDetails = result.getJSONArray("ListDetails")
                val length = ListDetails.length()
                for (i in 0 until length) {
                    val objJson = ListDetails.getJSONObject(i)
                    val ID = objJson.getString("ID")
                    val UserName = objJson.getString("UserName")
                    val Image = objJson.getString("Image")
                    val UserID = objJson.getString("UserID")
                    var Date = objJson.getString("Date")
                    Date = Date.replace("12:00:00 AM","")
                    mylist.add(Model_class_one(ID, UserName, Image, UserID,Date))
                }
                notifyDataSetChanged()
                currentpage = currentpage + 1

            } catch (e: JSONException) {
                e.printStackTrace()
            }
        }, Response.ErrorListener { error ->
        }) {
    }
    requestQueue.add(stringRequest)

}

override fun onCreateViewHolder(parent: ViewGroup, p1: Int): Gallery_adm_adp.Viewholder {
    val layout = LayoutInflater.from(fragmentActivity as Context?).inflate(R.layout.gallery_adp_admin, parent, false)
    loadmore = layout.findViewById(R.id.loadmore)
    image = layout.findViewById(R.id.image)
    return Viewholder(layout)
}

override fun getItemCount(): Int {
    return mylist.size
}

override fun onBindViewHolder(holder: Gallery_adm_adp.Viewholder, position: Int) {
              val Values = mylist[position]
                holder.mydata(Values, position)
              }

              }