I'm facing a problem using a Samsung J1 device here.
But the image taken is bigger than the preview one.
This is the preview that I got:
and this is the final bitmap result:
I'm calling camera view with this attributes:
<com.otaliastudios.cameraview.CameraView
android:id="#+id/camera"
app:cameraGesturePinch="zoom"
app:cameraFlash="auto"
app:cameraAutoFocusResetDelay="0"
app:cameraGestureTap="focusWithMarker"
android:keepScreenOn="true"
app:cameraPreview="glSurface"
app:cameraPictureSizeSmallest="true"
android:adjustViewBounds="true"
app:cameraGestureScrollHorizontal="exposureCorrection"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I already tried to use cameraPictureSizeAspectRatio with 4:3 but no success with it.
Did I do something wrong?
i have same problem .
after save image from cameraview ( i use camerakit lib) , display file by convert it to bitmap in imageview .
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraKitView = findViewById(R.id.camera)
val captureButton = findViewById<View>(R.id.button_capture) as Button
val imageView = findViewById<ImageView>(R.id.myImageView)
captureButton.setOnClickListener {
cameraKitView!!.captureImage { _, capturedImage ->
val savedPhoto = getOutputMediaFile()
try {
val outputStream = FileOutputStream(savedPhoto!!.path)
outputStream.write(capturedImage)
outputStream.close()
val myBitmap = BitmapFactory.decodeFile(savedPhoto.getAbsolutePath())
imageView.setImageBitmap(myBitmap)
} catch (e: java.io.IOException) {
e.printStackTrace()
}
}
}
}
and this is my layout :
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.camerakit.CameraKitView
android:id="#+id/camera"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:adjustViewBounds="true"/>
<Button
android:id="#+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="#string/save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/myImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:alpha="0.5"
android:adjustViewBounds="true"
android:background="#android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="#string/app_name" />
</android.support.constraint.ConstraintLayout>
but when show image and want to take another image, cameraview is not as scale as imageview
Solved :
just change scaleType to CentreCrop . that is it
Related
I am receiving a byteArray of my image over a socket from a localserver. the byteArray is represented as RGB. I'm converting it to bitmap. I checked that the bitmap is not empty. but when I use ImageView.setImageBitmap(bitmap) then my android app crashes. I've already tried a lot of things and I can't find a solution yet. help me please
class Cam : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("Cam activity", "onCreate")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cam)
val button_back : Button = findViewById(R.id.button_back)!!
var textView : TextView = findViewById(R.id.textView)!!
var imageView: ImageView = findViewById(R.id.Image_update)!!
Thread {
Thread.sleep(1000)
var byteArray = ByteArray(76032)
Connection.inBytesCam?.read(byteArray)
var nrOfPixels = byteArray.size / 3 // Three bytes per pixel.
var pixels = IntArray(nrOfPixels)
for (i in 0 until nrOfPixels){
var r = byteArray.get(3*i).toInt()
var g = byteArray.get(3*i + 1).toInt()
var b = byteArray.get(3*i + 2).toInt()
pixels.set(i, Color.rgb(r, g, b))
}
var bitmap = Bitmap.createBitmap(pixels, 176, 144, Bitmap.Config.ARGB_8888)
imageView.setImageBitmap(bitmap)
}.start()
button_back.setOnClickListener {
Thread {
runOnUiThread {
val intent = Intent(this#Cam, ControlActivity::class.java)
startActivity(intent)
}
}.start()
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/purple_200"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-103dp">
<Button
android:id="#+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/purple_700"
android:fontFamily="monospace"
android:text="Back"
android:textAlignment="center"
android:textSize="34sp"
app:iconTint="#color/purple_700"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.862" />
<TextView
android:id="#+id/textView"
android:layout_width="246dp"
android:layout_height="135dp"
android:layout_marginBottom="52dp"
android:text="LOG"
android:textAlignment="textStart"
app:layout_constraintBottom_toTopOf="#+id/button_back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/Image_update"
android:layout_width="176dp"
android:layout_height="144dp"
android:layout_marginTop="100dp"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/camera_recording" />
</androidx.constraintlayout.widget.ConstraintLayout>
i use DataInputStream/DataOutputStream to transfer ImageBytes from localserver to client on android.
i cant find anything that solve my problem
in general, I need to have an image update in a loop, like video player. but I can't even update one image :(
Use Glide library as below
add dependency;
implementation 'com.github.bumptech.glide:glide:4.11.0'
use in your activity etc.
Glide.with(context)
.load(bitmap)
.into(imageView)
I am trying to capture image within the rectangle as shown in image, using CameraX.
The way I am going on about this is, by having the ratio of Screen to Caputred image calculated and then mapping the rectangle visible in camera to another rectangle(equivalent to the captured image) and then cropping the bitmap. The code is as follows.
fragment_scan_doc.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/clRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gdl_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="28dp" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_25dp"
android:text="#string/app_name"
android:textColor="#color/black"
android:textSize="#dimen/text_size_24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_16dp"
android:text="#string/place_doc"
android:textColor="#color/black"
app:layout_constraintEnd_toEndOf="#+id/gdl_right"
app:layout_constraintStart_toStartOf="#+id/gdl_left"
app:layout_constraintTop_toBottomOf="#+id/tvTitle" />
<androidx.camera.view.PreviewView
android:id="#+id/previewView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="#dimen/dimen_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvDesc" />
<TextView
android:id="#+id/tvCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:background="#drawable/bg_round_white_solid_yellow_stripe"
android:gravity="center"
android:paddingStart="#dimen/dimen_25dp"
android:paddingTop="#dimen/dimen_10dp"
android:paddingEnd="#dimen/dimen_25dp"
android:paddingBottom="#dimen/dimen_10dp"
android:text="Front"
android:textColor="#color/black"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toTopOf="#+id/previewView" />
<View
android:id="#+id/border_view"
android:layout_width="325dp"
android:layout_height="202.5010436dp"
android:background="#drawable/bg_round_grey_stripe"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvPoweredBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Powered by"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="#id/gdl_right"
app:layout_constraintStart_toStartOf="#id/gdl_left"
app:layout_constraintTop_toBottomOf="#+id/border_view" />
<View
android:id="#+id/vTakePhoto"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="20dp"
android:background="#drawable/bg_round_white_solid_stripe"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<ImageView
android:id="#+id/ivFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:background="#drawable/bg_circle_white_solid"
android:padding="10dp"
android:src="#drawable/ic_baseline_flash_off_24"
app:layout_constraintBottom_toBottomOf="#+id/vTakePhoto"
app:layout_constraintStart_toEndOf="#+id/vTakePhoto"
app:layout_constraintTop_toTopOf="#+id/vTakePhoto" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="50dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gdl_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="28dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the place where I get Image from camerax and I submit it to the cropImage() function
private fun takePhoto() {
imageCapture?.takePicture(ContextCompat.getMainExecutor(requireContext()),
object : ImageCapture.OnImageCapturedCallback() {
#SuppressLint("UnsafeOptInUsageError")
override fun onCaptureSuccess(imageProxy: ImageProxy) {
val image = imageProxy.image?.let { image ->
InputImage.fromMediaImage(
image,
imageProxy.imageInfo.rotationDegrees
)
}
cropImage(imageProxy)
}
override fun onError(exception: ImageCaptureException) {
exception.printStackTrace()
}
})
}
This is the cropImage() function
private fun cropImage(imageProxy: ImageProxy) {
val ori = imageProxy.convertImageProxyToBitmap().rotate(90f)
val rect = Rect()
mViewBinding.borderView.getGlobalVisibleRect(rect)
val screenWidth = mViewBinding.root.width
val screenHeight = mViewBinding.root.height
val ratioW = screenWidth.toFloat() / ori.width // I am trying to get the ratio of screen to image width
val ratioH = screenHeight.toFloat() / ori.height
val x1 = rect.left / ratioW
val y1 = rect.top / ratioH
val x1W = rect.right / ratioW
val x1H = rect.bottom / ratioH
val rect1 = Rect(x1.toInt(), y1.toInt(), x1W.toInt(), x1H.toInt())
val cropedBm = Bitmap.createBitmap(
ori,
rect1.left,
rect1.top,
rect1.right - rect1.left,
rect1.bottom - rect1.top
)
processNewBitmap(cropedBm) // this is where I display the cropped bitmap and it appears like in the image below
}
The image is cropped a bit below the rectangle frame.
I am not quite sure what I am doing wrong. Any help or direction would be highly appreciated. Thanks
your ImageProxy has a bitmap you want to crop with a view overlay that does not same width or height. You must calculate with scale ratio.
You can refer to this link:
https://www.codeproject.com/Articles/1276135/Crop-Image-from-Camera-on-Android
I'm developing an app. In this app there's an alertdialog that opens when you click a point.
In this alert dialog there are two edittext, a gridview and a button. The gridview has associated a BaseAdapter that convert Uris to IconView (a class that extends ImageView) using Glide.
I want to set a maximum height of 350dp, but if there is just one row (there'll be always one image) I want the gridview to adapt.
I tried this way: How to have a GridView that adapts its height when items are added
But it doesn't work. So what I've tried is to count the number of elements and if there's is more than 2, set 350dp of height if there is less than 2, set 175dp. But it doesnt work.
The alert dialog layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/puntoName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/puntoDescripcion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:maxHeight="100dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/puntoName" />
<GridView
android:id="#+id/photo_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:horizontalSpacing="6dp"
android:numColumns="2"
android:verticalSpacing="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/puntoDescripcion"></GridView>
<Button
android:id="#+id/cerrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cerrar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/photo_grid" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code I'm using in order to change GridView height:
fun checkSize() {
val popUp: View = fragmentCaller.layoutInflater.inflate(R.layout.popup,null)
var photogrid: GridView = popUp.findViewById(R.id.photo_grid)
var params: ViewGroup.LayoutParams = photogrid.layoutParams
var tam: Int = 175
if (adapter.getDataSource().size>2) {
tam = DpToPixels(350)
}
photogrid.layoutParams.height = tam
photogrid.requestLayout()
}
private fun DpToPixels(dp: Int) : Int {
val escala: Float = fragmentCaller.requireContext().resources.displayMetrics.density;
var tam: Int = (dp * escala + 0.5f).toInt()
return tam
}
The size of the gridView is always 0. I don't know why. I just want to set android:layout_height programmatically. I call checkSize() method after I call Dialog.show() and everytime I do changes in the elements of adapter.
Thanks.
I have a constraint layout inside a scroll view. and inside that constraint layout I have a support map fragment.
I am trying to find what makes my app render slowly. it takes around 1.5 second to show a fragment that contains a map (support map fragment). I mean, when I replace fragment A to fragment B (that has map) it will takes too long, after 1-2 second, then the fragment B will appear on the screen.
here is my xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" android:id="#+id/scrollView_event_detail">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Fragments.Reusable.EventDetailFragment"
android:id="#+id/constraintLayout_event_detail_destination">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" tools:src="#tools:sample/backgrounds/scenic[12]"
android:id="#+id/imageView_event_detail_blurred_poster"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:scaleType="centerCrop" app:layout_constraintDimensionRatio="1:1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" tools:src="#tools:sample/backgrounds/scenic[12]"
android:id="#+id/imageView_event_detail_poster"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintDimensionRatio="1:1"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="#+id/textView_event_detail_event_title"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/imageView_event_detail_blurred_poster"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp" android:maxLines="2"
android:textSize="18sp" tools:text="Kajian Bulanan: Memahami Adab Para Penuntut Ilmu"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="#+id/line1_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_event_title"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<Button
android:text="Hadir"
android:layout_width="0dp"
android:layout_height="50dp" android:id="#+id/button_search_event_search"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/line1_view_event_detail"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
android:background="#drawable/rounded_button"
android:textColor="#ffffff"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="#+id/line2_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/button_search_event_search"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_date_time"
android:id="#+id/imageView_event_detail_icon_dateTime"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="#+id/line2_view_event_detail" android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_date_label"
app:layout_constraintTop_toTopOf="#+id/imageView_event_detail_icon_dateTime"
app:layout_constraintBottom_toBottomOf="#+id/imageView_event_detail_icon_dateTime"
app:layout_constraintStart_toEndOf="#+id/imageView_event_detail_icon_dateTime"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Selasa, 23 Desember 2018 - Rabu, 24 Desember 2018"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_time_label"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_date_label"
app:layout_constraintStart_toStartOf="#+id/textView_event_detail_date_label"
tools:text="17.00 - 21.00 WIB"
android:maxLines="1"
android:layout_marginTop="4dp" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_place"
android:id="#+id/imageView_event_detail_icon_address"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_time_label"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_venue_label"
app:layout_constraintTop_toTopOf="#+id/imageView_event_detail_icon_address"
app:layout_constraintBottom_toBottomOf="#+id/imageView_event_detail_icon_address"
app:layout_constraintStart_toEndOf="#+id/imageView_event_detail_icon_address"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Universitas Gadjah Mada"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_address_label"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_venue_label"
app:layout_constraintStart_toStartOf="#+id/textView_event_detail_venue_label"
tools:text="Jalan Grafika no.1. Sinduadi Sleman. Yogyakarta"
android:maxLines="1"
android:layout_marginTop="4dp" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_person"
android:id="#+id/imageView_event_detail_icon_speaker"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_address_label" android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_speaker_label"
app:layout_constraintTop_toTopOf="#+id/imageView_event_detail_icon_speaker"
app:layout_constraintBottom_toBottomOf="#+id/imageView_event_detail_icon_speaker"
app:layout_constraintStart_toEndOf="#+id/imageView_event_detail_icon_speaker"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Pembicara: Ustadz Khalid Basalamah"
android:maxLines="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_monetization"
android:id="#+id/imageView_event_detail_icon_price"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_speaker_label"
android:layout_marginTop="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_price_label"
app:layout_constraintTop_toTopOf="#+id/imageView_event_detail_icon_price"
app:layout_constraintBottom_toBottomOf="#+id/imageView_event_detail_icon_price"
app:layout_constraintStart_toEndOf="#+id/imageView_event_detail_icon_price"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Rp 50.000"
android:maxLines="1"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="#+id/line3_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_price_label"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_description_snippet_label"
app:layout_constraintTop_toTopOf="#+id/line3_view_event_detail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" tools:text="Kajian akan membahas kitab Riyadush Shalihin Bab 3"
android:maxLines="2"/>
<TextView
android:text="Baca Selengkapnya"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView_button_read_more"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_description_snippet_label"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true" android:textColor="#2196F3"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="#+id/line4_view_event_detail" android:background="#696C79"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/textView_button_read_more"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="180dp"
android:id="#+id/map_event_detail"
tools:context=".Activities.MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/line4_view_event_detail"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:text="Peta Ke Lokasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView_button_map_more"
app:layout_constraintTop_toBottomOf="#+id/map_event_detail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:clickable="true"
android:focusable="true" android:textColor="#2196F3"/>
<View
android:layout_width="0dp"
android:layout_height="0.5dp" android:id="#+id/line5_view_event_detail" android:background="#696C79"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/textView_button_map_more"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"/>
<TextView
android:text="Penyelenggara"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_organizer_tag"
app:layout_constraintTop_toBottomOf="#+id/line5_view_event_detail"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"/>
<TextView
android:text="Masjid Nurul Iman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView_event_detail_organizer_label"
app:layout_constraintTop_toBottomOf="#+id/textView_event_detail_organizer_tag"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have tried to comment out all codes in my onCreateView. so this fragment will do nothing. and still, my app render slowly when showing this fragment.
but when I try to remove support map fragment from my xml, I mean this part:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="180dp"
android:id="#+id/map_event_detail"
tools:context=".Activities.MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/line4_view_event_detail"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"/>
it will make my app render faster. so why this is happened or is it any alternatives to solve this ?
I mean, to show a map in the constraint layout inside a scroll view ?
here is my code:
class EventDetailFragment : Fragment() {
lateinit var titleTextView: TextView
lateinit var fragmentView : View
lateinit var eventPosterImageView : ImageView
lateinit var eventBlurredPosterImageView: ImageView
lateinit var eventTitleTextView : TextView
lateinit var attendanceButton: Button
lateinit var dateTextView: TextView
lateinit var timeTextView : TextView
lateinit var venueTextView: TextView
lateinit var addressTextView: TextView
lateinit var speakerTextView: TextView
lateinit var priceTextView: TextView
lateinit var descriptionSnippetTextView: TextView
lateinit var readMoreTextViewButton : TextView
lateinit var mapMoreTextViewButton : TextView
lateinit var organizerTagTextView: TextView
lateinit var organizerLabelTextView: TextView
lateinit var mapFragment : SupportMapFragment
lateinit var googleMap: GoogleMap
lateinit var mContext : Context
lateinit var mActivity : FragmentActivity
lateinit var selectedEvent : Event
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
activity?.let { mActivity = it }
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentView = inflater.inflate(R.layout.fragment_event_detail, container, false)
// setUpViewsDeclaration()
// setUpSafeArg()
// setUpListeners()
// updateUI()
return fragmentView
}
private fun setUpViewsDeclaration() {
titleTextView = mActivity.findViewById(R.id.destination_label_text_view)
eventPosterImageView = fragmentView.findViewById(R.id.imageView_event_detail_poster)
eventBlurredPosterImageView = fragmentView.findViewById(R.id.imageView_event_detail_blurred_poster)
eventTitleTextView = fragmentView.findViewById(R.id.textView_event_detail_event_title)
attendanceButton = fragmentView.findViewById(R.id.button_search_event_search)
dateTextView = fragmentView.findViewById(R.id.textView_event_detail_date_label)
timeTextView = fragmentView.findViewById(R.id.textView_event_detail_time_label)
venueTextView = fragmentView.findViewById(R.id.textView_event_detail_venue_label)
addressTextView = fragmentView.findViewById(R.id.textView_event_detail_address_label)
speakerTextView = fragmentView.findViewById(R.id.textView_event_detail_speaker_label)
priceTextView = fragmentView.findViewById(R.id.textView_event_detail_price_label)
descriptionSnippetTextView = fragmentView.findViewById(R.id.textView_event_detail_description_snippet_label)
readMoreTextViewButton = fragmentView.findViewById(R.id.textView_button_read_more)
organizerTagTextView = fragmentView.findViewById(R.id.textView_event_detail_organizer_tag)
organizerLabelTextView = fragmentView.findViewById(R.id.textView_event_detail_organizer_label)
mapMoreTextViewButton = fragmentView.findViewById(R.id.textView_button_map_more)
}
private fun setUpSafeArg() {
arguments?.let {
val args = EventDetailFragmentArgs.fromBundle(it)
selectedEvent = args.selectedEvent
}
}
private fun setUpListeners() {
eventPosterImageView.setOnClickListener {
val photoViewNavigation = EventDetailFragmentDirections.actionGlobalPhotoViewFragment(selectedEvent.posterDownloadPath)
Navigation.findNavController(fragmentView).navigate(photoViewNavigation)
}
eventBlurredPosterImageView.setOnClickListener {
val photoViewNavigation = EventDetailFragmentDirections.actionGlobalPhotoViewFragment(selectedEvent.posterDownloadPath)
Navigation.findNavController(fragmentView).navigate(photoViewNavigation)
}
readMoreTextViewButton.setOnClickListener {
val nextDestination = EventDetailFragmentDirections.actionToDescriptionDetail(selectedEvent.description)
Navigation.findNavController(fragmentView).navigate(nextDestination)
}
mapMoreTextViewButton.setOnClickListener {
val latitudeFloat = selectedEvent.coordinate.latitude.toFloat()
val longitudeFloat = selectedEvent.coordinate.longitude.toFloat()
val mapDetailDestination = EventDetailFragmentDirections.actionToMapDetailLocation(latitudeFloat,longitudeFloat)
Navigation.findNavController(fragmentView).navigate(mapDetailDestination)
}
}
private fun updateUI() {
Picasso.get()
.load(selectedEvent.posterDownloadPath)
.into(eventPosterImageView)
Picasso.get()
.load(selectedEvent.posterDownloadPath)
.transform(BlurTransformation(mContext,25,3))
.into(eventBlurredPosterImageView)
eventTitleTextView.text = selectedEvent.title
val dateStartingDate = DateTimeService.changeDateToString("EEEE, d MMMM yyyy",selectedEvent.dateTimeStart)
val timeStartingDate = DateTimeService.changeDateToString("HH:mm",selectedEvent.dateTimeStart)
val dateEndingDate = DateTimeService.changeDateToString("EEEE, d MMMM yyyy",selectedEvent.dateTimeFinish)
val timeEndingDate = DateTimeService.changeDateToString("HH:mm zzz",selectedEvent.dateTimeFinish)
dateTextView.text = dateStartingDate
timeTextView.text = "$timeStartingDate - $timeEndingDate"
venueTextView.text = selectedEvent.venue
addressTextView.text = selectedEvent.address
speakerTextView.text = "Penceramah: ${selectedEvent.speaker}"
organizerLabelTextView.text = selectedEvent.creatorFullName
descriptionSnippetTextView.text = selectedEvent.description
setUpGoogleMap()
}
private fun setUpGoogleMap() {
mapFragment = childFragmentManager.findFragmentById(R.id.map_event_detail) as SupportMapFragment
mapFragment.getMapAsync {
googleMap = it
val eventLocation = LatLng(selectedEvent.coordinate.latitude,selectedEvent.coordinate.longitude)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(eventLocation,16f))
googleMap.addMarker(MarkerOptions().position(eventLocation).title("Lokasi Acara")).showInfoWindow()
googleMap.setOnMapClickListener {
val latitudeFloat = selectedEvent.coordinate.latitude.toFloat()
val longitudeFloat = selectedEvent.coordinate.longitude.toFloat()
val mapDetailDestination = EventDetailFragmentDirections.actionToMapDetailLocation(latitudeFloat,longitudeFloat)
Navigation.findNavController(fragmentView).navigate(mapDetailDestination)
}
}
}
}
I would suggest the use of MapView instead of the SupportMapFragment as you would have better control over the lifecycle of the map.
I think that you're unable to replace the fragment out since SupportMapFragment integrates the Map's lifecycle with the Activity and you're unable to replace it until the Map has completed loading.
MapView will give you better control over your map as you should be able to get your fragment replaced even if the map hasn't loaded
Check out this answer ( explains the difference better than me ) :- Differences between MapView, MapFrament and SupportMapFragment
you have to much widget and element to show , i think you are facing a jank , from the official documentation
UI Rendering is the act of generating a frame from your app and displaying it on the screen. To ensure that a user's interaction with your app is smooth, your app should render frames in under 16ms to achieve 60 frames per second (why 60fps?). If your app suffers from slow UI rendering, then the system is forced to skip frames and the user will perceive stuttering in your app. We call this jank.
i advice to read carefully the documentation to get better understanding of how to make UI smooth
Hello everyone i have little problem about shared element.
I have using shared element at activity transition.
When click to recyclerView item that view comes to front of all view.
as you can see when i click to lowermost picture
begin transition fragment --> activity
private fun itemClicked(pos: Int, view: View) {
val intent = Intent(context, DetailActivity::class.java)
val title = view.findViewById<TextView>(R.id.title)
val photo = view.findViewById<ImageView>(R.id.photo)
intent.putExtra(IMAGE_TRANSITION_NAME, photo.transitionName)
intent.putExtra(TITLE_TRANSITION_NAME, title.transitionName)
val pair1 =
Pair.create(
photo as View,
ViewCompat.getTransitionName(photo).toString()
)
val pair2 =
Pair.create(
title as View,
ViewCompat.getTransitionName(title).toString()
)
val options = activity?.let {
ActivityOptionsCompat.makeSceneTransitionAnimation(it, pair2, pair1)
}
startActivity(intent, options?.toBundle())
}
my custom view holder
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
app:contentPaddingBottom="0dp"
app:contentPaddingLeft="0dp"
app:contentPaddingRight="0dp"
app:contentPaddingTop="0dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:maxLines="2"
app:autoSizeMaxTextSize="#dimen/primary_text_medium"
app:autoSizeMinTextSize="#dimen/primary_text_small"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/photo"
tools:text="Deniz subaşı" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>