Skip to content

Instantly share code, notes, and snippets.

@yunusemredilber
Created February 15, 2020 11:53
Show Gist options
  • Save yunusemredilber/b03d46f11a7eddbfc7bc37ab527c7545 to your computer and use it in GitHub Desktop.
Save yunusemredilber/b03d46f11a7eddbfc7bc37ab527c7545 to your computer and use it in GitHub Desktop.
Android circular image view downloaded from the internet with AsyncTask
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity"
android:id="@+id/main_view_map">
<androidx.cardview.widget.CardView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:id="@+id/roundCardView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="16dp"
android:layout_centerHorizontal="true"
android:elevation="9dp"
app:cardCornerRadius="20dp">
<ImageView
android:id="@+id/profile_button"
android:elevation="10dp"
android:background="@drawable/circulo"
android:scaleType="centerCrop"
android:layout_width="40dp"
android:layout_height="40dp" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:angle="270"
android:color="@color/grey" />
</shape>
public class MapActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Download and set image of profile button. (async)
new DownloadButtonImageTask().execute(
new DownloadButtonImageTaskParams("https://images.unsplash.com/photo-1555436169-f998ad1259fd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80",
findViewById(R.id.profile_button)));
}
}
class DownloadButtonImageTaskParams {
String url;
ImageView profileButton;
DownloadButtonImageTaskParams(String url, ImageView profileButton) {
this.url = url;
this.profileButton = profileButton;
}
}
class DownloadButtonImageTask extends AsyncTask<DownloadButtonImageTaskParams, Void, Drawable> {
DownloadButtonImageTaskParams downloadButtonImageTaskParams;
protected Drawable doInBackground(DownloadButtonImageTaskParams... params) {
downloadButtonImageTaskParams = params[0];
Bitmap x;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(downloadButtonImageTaskParams.url).openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
connection.connect();
} catch (IOException e) {
e.printStackTrace();
}
InputStream input = null;
try {
input = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
x = BitmapFactory.decodeStream(input);
return new BitmapDrawable(Resources.getSystem(), x);
}
protected void onPostExecute(Drawable res) {
downloadButtonImageTaskParams.profileButton.setImageDrawable(res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment