I am trying to build a gallery for my app. Following some tutorials and stackoverflow, I finally managed to make the gallery. I am using Glide to set images, so no issue of memory crashing and all. The only problem is, when I fling, Images are not set into their views as fast as those views are being shown. Here is a gif. So I get to see the placeholders or the background of the recyclerview while imageviews are being set. Depending on the fling speed, this may last for 500-1000ms. But that is sufficient to make the gallery look bad. This happens when I use fling to scroll up or down. To my knowledge, this is because glide freeing the images from it's cache. Is there any solution for it.
Here is my code.
activity_gallery:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/activity_monuments"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GalleryActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:alwaysDrawnWithCache="true"
android:clipToPadding="false"
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay"/>
</LinearLayout>
Here is how I am setting recyclerview:
RecyclerView gridView = (RecyclerView)findViewById(R.id.recyclerview_gallery);
gridView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),NUM_OF_COLUMNS);
gridView.setLayoutManager(layoutManager);
gridView.isDrawingCacheEnabled();
gridView.addItemDecoration(new MarginDecoration(GalleryActivity.this, NUM_OF_COLUMNS, GRID_PADDING, true));
gridView.setHasFixedSize(true);
gridView.setVerticalScrollBarEnabled(true);
gridView.setBackgroundColor(getResources().getColor(R.color.colorBlack));
GalleryAdapter galleryAdapter = new GalleryAdapter(GalleryActivity.this, GalleryActivity.this, ImageNamesList, columnWidth);
gridView.setAdapter(galleryAdapter);
Here is how I am setting imageview in adapter:
viewHolder.galleryImage.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
Glide.with(context)
.load(uri)
.asBitmap()
.placeholder(R.drawable.monument)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(viewHolder.galleryImage);
Please let me know of the possible solutions.
P.S: The gif is low frame rate and hence showing the slow fling which is not the case.
To solve cache management, i use a singleton store best practice. Realm for android is perfect it is NoSql and super fast, what i normally do is create a realm object, in the adapter check if the current url is in realm, if true dont pull new image else, pull and store.
Then when the main activity or fragment is ever being destroy just delete all from realm.
Related
The starting point here is the need to periodically rotate a vector drawing, like for instance the hands of an analog clock.
I understand that a vectorDrawable resource does not directly support rotation attributes. I can put the drawable inside a group which can rotate the drawable, but that rotation won't be modifiable at run time.
I came across many posts explaining rotating views or canvases, as well as the rotate drawable. The latter seems to be appropriate for my purpose.
I created a new activity with the help of Android Studio 3.5 to achieve a simple rotation with the help of a rotate drawable.
java/com.example.myapplication/MainActivity:
package com.example.myapplication
import android.graphics.drawable.RotateDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val activity = this#MainActivity
val context = activity.applicationContext
val drawable = ContextCompat.getDrawable(context, R.drawable.rotation)
val rotation = drawable as RotateDrawable
rotation.setLevel(180)
}
}
res/layout/activity_main.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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView2"
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"
app:srcCompat="#drawable/rotation" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/drawable/rotation.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="0" android:toDegrees="360"
android:drawable="#drawable/ic_launcher_foreground"/>
I expect that the setLevel function can be used to rotate the drawable, but it has no effect.
Changing the fromDegrees attribute in the rotation.xml does the rotation as expected. I am not sure if the maximum level for <rotate> should be 360 or 10000, but that is not the issue.
What am I missing? Is it a detail, or is the rotateDrawable the wrong approach altogether?
Related questions:
Rotating a drawable in Android
Programmatically rotate drawable or view
Is it possible to rotate a drawable in the xml description?
Try using imageView2.setRotation(180) instead of rotation drawable.
I have a VB WinForms application, I got frames from somewhere. App is not developed by me.
_ImageList = New Queue(Of MemoryStream)
Dim bmpdecode As New Bitmap(nWidth, nHeight)
Dim ms As New MemoryStream
bmpdecode.Save(ms, Imaging.ImageFormat.Jpeg)
_ImageList.Enqueue(ms)
PictureBox1.BeginInvoke(New DelPintarImagen(AddressOf DrawImage), New Object() {bmpdecode})
DrawImage is a bitmap type.
What I want is to get from a Xamarin android app VideoView or ImageView what I see in PictureBox1
After a days of research I have not been able to find anything that works minimally.
I can see the stream by http in web browser but I can't get from any VideoView or ImageView.
I got a solution for this, it's simple but it works.
I used a WebView to display the stream in my Xamarin App disabling the zoom option, so for the user, it seems like a stream playing.
C# code:
var wPlayer = FindViewById<WebView>(Resource.Id.webView1);
//Zoom Out for stretch the content by the width.
wPlayer.Settings.LoadWithOverviewMode = true;
wPlayer.Settings.UseWideViewPort = true;
wPlayer.SetPadding(0, 0, 0, 0);
wPlayer.LoadUrl("http://ip:port/?params");
Axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main">
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView1"
/>
</RelativeLayout>
I've been looking in Stackoverflow how to integrate an AdView inside a RecyclerView. I've been following these posts:
One, two
Basically the way to do it is calling loadAd inside onCreateViewHolder or inside the constructor of the ViewHolder.
Either way, this is my implementation:
JAVA
public class AdExpressViewHolder extends RecyclerView.ViewHolder {
public AdExpressViewHolder(View itemView) {
super(itemView);
final AdView adView = (AdView)itemView.findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.build();
adView.loadAd(request);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
ads:adUnitId="**********************"
ads:adSize="BANNER">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
</RelativeLayout>
The problem is: when I scroll the RecyclerView, it seems to load on the UI thread since it gets stuck, only the first time. The rest of the times is ok.
This is the video that demonstrates it:
Video
As you can see, the first one is blocking the UI, but not the second one.
What am I doing wrong?
Thank you in advance.
EDIT
I've tried to load a conventional AdView in an activity, fixed. It works and it doesn't seem to be load in the UI Thread. Seems it's just happening in the RecyclerView.
After 3 weeks, I've done a Method profiling, and this is what I've got out:
You can realise the red spots. Those are 2 different AdView loading, while the rest are 38 normal custom views of mine.
To be more concrete, these are the functions, so it's regarding 100% the AdView:
It seems a bug of the Ads SDK for Android and it's not been fixed, at least until v9.4.0.
More information here:
https://groups.google.com/forum/#!searchin/google-admob-ads-sdk/ui$20thread%7Csort:relevance/google-admob-ads-sdk/k4IFZA_QGT4/3gMHaCPPBQAJ
I'm new to Android programming and I'm facing a problem: I don't know how to set the position of an imageView over another imageView. I want the position to be relative, maybe with percentages, to maintain the aspect on different screens or in both landscape and portrait.
thank you!
to place an Image over another use relativelayout as the root layout for both image views. This will make both image views start at the same coordinate and you can flexibly arrange them as you like.
example :
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<ImageView android:id="#+id/videoImage"
android:layout_width="match_parent"
android:layout_height="170dip"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<ImageView
android:id="#+id/playVideo"
android:src="#drawable/play_"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"/>
Alternatively you can use frame layout.
When i tried to add a fragment inside a dialog, the app got crash. The crash saying "No View found for ID 0x01276"
This is the layout file for Dialog (my_dialog_layout.axml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And this is the code for opening the dialog and for fragment transaction
class CustomDialog : Dialog{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState)
SetContentView(Resource.Layout.dialog_fragment_layout);
var myCustomFragmnent = new MyCustomFragment(_context);
// Start Fragment Transaction Process
var transaction = FragmentManager.BeginTransaction();
// Here is a crash saying (No View found for ID 0x01276....)
transaction.Add(Resource.Id.fragment_container, myCustomFragmnent);
transaction.Commit();
}
}
Firstly you don't need to use a "heavy" layout such as LinearLayout, I suggest you use FrameLayout for your container.
Secondly, try to use transaction.Replace instead. Also make sure that MyCustomFragment does not blow up in OnCreateView. It might be where your problem lies as you didn't post the full stack trace.
When using transaction.Replace you can have it handle the backstack for you by adding:
transaction.AddToBackStack(null);
after your Replace call, such that when you press the back button on your phone it navigates back to the previous Fragment shown.