Commit 9f0974fa authored by Divyanshu Bhargava's avatar Divyanshu Bhargava

show drawing view

parent 7c56cc4d
package chat.rocket.android.chatroom.ui package chat.rocket.android.chatroom.ui
import android.app.Activity import android.app.Activity
import android.app.Dialog
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.provider.MediaStore
import android.support.annotation.DrawableRes import android.support.annotation.DrawableRes
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v7.widget.DefaultItemAnimator import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.view.* import android.view.*
import android.widget.ImageView
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.chatroom.adapter.* import chat.rocket.android.chatroom.adapter.*
import chat.rocket.android.chatroom.presentation.ChatRoomPresenter import chat.rocket.android.chatroom.presentation.ChatRoomPresenter
...@@ -27,6 +32,7 @@ import chat.rocket.android.helper.EndlessRecyclerViewScrollListener ...@@ -27,6 +32,7 @@ import chat.rocket.android.helper.EndlessRecyclerViewScrollListener
import chat.rocket.android.helper.KeyboardHelper import chat.rocket.android.helper.KeyboardHelper
import chat.rocket.android.helper.MessageParser import chat.rocket.android.helper.MessageParser
import chat.rocket.android.util.extensions.* import chat.rocket.android.util.extensions.*
import chat.rocket.android.widget.CustomDrawView
import chat.rocket.android.widget.emoji.* import chat.rocket.android.widget.emoji.*
import chat.rocket.core.internal.realtime.socket.model.State import chat.rocket.core.internal.realtime.socket.model.State
import dagger.android.support.AndroidSupportInjection import dagger.android.support.AndroidSupportInjection
...@@ -35,7 +41,7 @@ import kotlinx.android.synthetic.main.fragment_chat_room.* ...@@ -35,7 +41,7 @@ import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_attachment_options.* import kotlinx.android.synthetic.main.message_attachment_options.*
import kotlinx.android.synthetic.main.message_composer.* import kotlinx.android.synthetic.main.message_composer.*
import kotlinx.android.synthetic.main.message_list.* import kotlinx.android.synthetic.main.message_list.*
import timber.log.Timber import java.io.ByteArrayOutputStream
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
import javax.inject.Inject import javax.inject.Inject
...@@ -620,6 +626,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -620,6 +626,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
} }
button_drawing.setOnClickListener{ button_drawing.setOnClickListener{
showDrawingView()
handler.postDelayed({ handler.postDelayed({
hideAttachmentOptions() hideAttachmentOptions()
}, 400) }, 400)
...@@ -627,6 +634,31 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -627,6 +634,31 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
} }
} }
private fun showDrawingView() {
val drawDialog = Dialog(activity,R.style.DrawingDialogStyle)
drawDialog.setContentView(R.layout.drawing_view)
val customDrawView: CustomDrawView = drawDialog.findViewById(R.id.custom_draw_view)
val closeDraw: ImageView = drawDialog.findViewById(R.id.image_close_drawing)
val sendDraw: ImageView = drawDialog.findViewById(R.id.image_send_drawing)
closeDraw.setOnClickListener {
drawDialog.dismiss()
}
sendDraw.setOnClickListener {
val bitmap: Bitmap = customDrawView.getBitmap()
uploadFile(bitmapToUri(bitmap))
dummy_image.setImageBitmap(bitmap)
drawDialog.dismiss()
}
drawDialog.show()
}
fun bitmapToUri(bitmap: Bitmap): Uri {
val bytes = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG,100,bytes)
val path = MediaStore.Images.Media.insertImage(activity?.contentResolver, bitmap, "title",null)
return Uri.parse(path)
}
private fun setupSuggestionsView() { private fun setupSuggestionsView() {
suggestions_view.anchorTo(text_message) suggestions_view.anchorTo(text_message)
.setMaximumHeight(resources.getDimensionPixelSize(R.dimen.suggestions_box_max_height)) .setMaximumHeight(resources.getDimensionPixelSize(R.dimen.suggestions_box_max_height))
......
package chat.rocket.android.widget
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
class CustomDrawView: View {
val strokeWidth = 4f
private var mX: Float = 0f
private var mY:Float = 0f
private lateinit var path: Path
private lateinit var paint: Paint
private val TOLERANCE = 5f
private lateinit var mBitmap: Bitmap
private lateinit var mCanvas: Canvas
fun init(){
path = Path()
paint = Paint()
paint.isAntiAlias = true
paint.color = Color.BLACK
paint.style = Paint.Style.STROKE
paint.strokeJoin = Paint.Join.ROUND
paint.strokeWidth = strokeWidth
this.isDrawingCacheEnabled = true
}
constructor(context: Context?) : super(context){ init() }
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){ init() }
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){ init() }
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.drawPath(path,paint)
}
private fun startTouch(x: Float,y: Float){
path.moveTo(x,y)
mX = x
mY = y
}
private fun moveTouch(x: Float,y: Float){
val dx: Float = Math.abs(x-mX)
val dy: Float = Math.abs(y-mY)
if (dx >= TOLERANCE || dy >= TOLERANCE){
path.quadTo(mX,mY,(x+mX)/2,(y+mY)/2)
mX = x
mY = y
}
}
private fun upTouch(){
path.lineTo(mX,mY)
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
mCanvas = Canvas(mBitmap)
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
val x: Float = event!!.x
val y: Float = event.y
when(event.action){
MotionEvent.ACTION_DOWN -> {
startTouch(x,y)
invalidate()
}
MotionEvent.ACTION_MOVE -> {
moveTouch(x,y)
invalidate()
}
MotionEvent.ACTION_UP -> {
upTouch()
invalidate()
}
}
return true
}
fun getBitmap(): Bitmap {
return this.drawingCache
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
xmlns:app="http://schemas.android.com/apk/res-auto">
<chat.rocket.android.widget.CustomDrawView
android:id="@+id/custom_draw_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/image_close_drawing"
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/ic_close_white_24dp"
android:tint="@color/colorPrimary"
android:padding="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/white"
android:foreground="?selectableItemBackground" />
<ImageView
android:id="@+id/image_send_drawing"
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/ic_send_24dp"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/white"
android:foreground="?selectableItemBackground" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
...@@ -130,4 +130,14 @@ ...@@ -130,4 +130,14 @@
tools:text="connected" tools:text="connected"
tools:visibility="visible" /> tools:visibility="visible" />
<ImageView
android:id="@+id/dummy_image"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:minHeight="100dp"
android:minWidth="100dp"
android:background="@color/white"/>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>
\ No newline at end of file
...@@ -109,4 +109,9 @@ ...@@ -109,4 +109,9 @@
<item name="actionModeCloseDrawable">@drawable/ic_close_white_24dp</item> <item name="actionModeCloseDrawable">@drawable/ic_close_white_24dp</item>
</style> </style>
<style name="DrawingDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">@color/white</item>
</style>
</resources> </resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment