From afae76b84e002c607b303c72c465e860efd9a4fe Mon Sep 17 00:00:00 2001 From: Divyanshu Bhargava Date: Fri, 18 May 2018 13:06:14 +0530 Subject: [PATCH] sample app --- .idea/caches/build_file_checksums.ser | Bin 584 -> 584 bytes .idea/vcs.xml | 6 ++ app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 2 + .../com/divyanshu/androiddraw/DrawAdapter.kt | 31 ++++++++++ .../com/divyanshu/androiddraw/MainActivity.kt | 55 +++++++++++++++++- app/src/main/res/layout/activity_main.xml | 18 ++++-- app/src/main/res/layout/item_view.xml | 12 ++++ app/src/main/res/values/colors.xml | 7 ++- app/src/main/res/values/styles.xml | 2 +- 10 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/divyanshu/androiddraw/DrawAdapter.kt create mode 100644 app/src/main/res/layout/item_view.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index e3d22c01b6f68fd8dffda5e6e921d96ae61efca5..3b0b2721fb5ad6f9791e63bb947ab90fc244fe49 100644 GIT binary patch delta 33 pcmX@Xa)M>T43>Px-Ul1!%w`mf+mLyVSCw_@mfF}bHRF2=D*)iIu-3S + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 73f8578..423a3a2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,4 +31,5 @@ dependencies { androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation project(':draw') + implementation 'com.android.support:design:27.1.1' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 49db966..646419d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + ) : RecyclerView.Adapter(){ + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view,parent,false) + return ViewHolder(view) + } + + override fun getItemCount(): Int { + return filePath.size + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val path = filePath[holder.adapterPosition] + holder.drawImage.setImageURI(Uri.parse(path)) + } + + class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) { + val drawImage: ImageView = itemView.findViewById(R.id.image_draw) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/divyanshu/androiddraw/MainActivity.kt b/app/src/main/java/com/divyanshu/androiddraw/MainActivity.kt index 2da484b..c35e6a8 100644 --- a/app/src/main/java/com/divyanshu/androiddraw/MainActivity.kt +++ b/app/src/main/java/com/divyanshu/androiddraw/MainActivity.kt @@ -1,9 +1,22 @@ package com.divyanshu.androiddraw +import android.app.Activity import android.content.Intent +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.net.Uri import android.support.v7.app.AppCompatActivity import android.os.Bundle +import android.os.Environment +import android.support.v7.widget.GridLayoutManager import com.divyanshu.draw.activity.DrawingActivity +import kotlinx.android.synthetic.main.activity_main.* +import java.io.File +import java.io.FileOutputStream +import java.util.* +import kotlin.collections.ArrayList + +private const val REQUEST_CODE_DRAW = 101 class MainActivity : AppCompatActivity() { @@ -11,7 +24,47 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - startActivity(Intent(this, DrawingActivity::class.java)) + recycler_view.layoutManager = GridLayoutManager(this,2) + recycler_view.adapter = DrawAdapter(getFilePath()) + + fab_add_draw.setOnClickListener { + val intent = Intent(this, DrawingActivity::class.java) + startActivityForResult(intent, REQUEST_CODE_DRAW) + } + } + + private fun getFilePath(): ArrayList{ + val resultList = ArrayList() + val imageDir = "${Environment.DIRECTORY_PICTURES}/Android Draw/" + val path = Environment.getExternalStoragePublicDirectory(imageDir) + val imageList = path.listFiles() + for (imagePath in imageList){ + resultList.add(imagePath.absolutePath) + } + return resultList + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (data != null && resultCode == Activity.RESULT_OK) { + when(requestCode){ + REQUEST_CODE_DRAW -> { + val result= data.getByteArrayExtra("bitmap") + val bitmap = BitmapFactory.decodeByteArray(result, 0, result.size) + saveImage(bitmap) + } + } + } + } + private fun saveImage(bitmap: Bitmap) { + val imageDir = "${Environment.DIRECTORY_PICTURES}/Android Draw/" + val path = Environment.getExternalStoragePublicDirectory(imageDir) + val file = File(path, UUID.randomUUID().toString()+".png") + path.mkdirs() + file.createNewFile() + val outputStream = FileOutputStream(file) + bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream) + outputStream.flush() + outputStream.close() } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 84f1951..b0f249c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,13 +6,21 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + + + android:layout_margin="16dp" + android:src="@android:drawable/ic_input_add" + android:tint="@color/color_white" /> \ No newline at end of file diff --git a/app/src/main/res/layout/item_view.xml b/app/src/main/res/layout/item_view.xml new file mode 100644 index 0000000..0b0be2b --- /dev/null +++ b/app/src/main/res/layout/item_view.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3f7d1a6..61655c2 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,6 +1,7 @@ - #000 - #000 - #1976D2 + #FFFFFF + #CCCCCC + #000 + #212121 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 5885930..9513160 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,7 @@ -