Skip to content

Commit

Permalink
Add showcase for XML types
Browse files Browse the repository at this point in the history
Reviewed By: oprisnik

Differential Revision: D64259734

fbshipit-source-id: f12358c4205d38ebc4cf4825b630002ac0e67688
  • Loading branch information
Abbondanzo authored and facebook-github-bot committed Oct 23, 2024
1 parent bf62ecb commit 23a16df
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.facebook.fresco.samples.showcase.imageformat.override.ImageFormatOver
import com.facebook.fresco.samples.showcase.imageformat.pjpeg.ImageFormatProgressiveJpegFragment
import com.facebook.fresco.samples.showcase.imageformat.svg.ImageFormatSvgFragment
import com.facebook.fresco.samples.showcase.imageformat.webp.ImageFormatWebpFragment
import com.facebook.fresco.samples.showcase.imageformat.xml.ImageFormatXmlFragment
import com.facebook.fresco.samples.showcase.imagepipeline.*
import com.facebook.fresco.samples.showcase.misc.WelcomeFragment
import com.facebook.fresco.samples.showcase.settings.SettingsFragment
Expand Down Expand Up @@ -56,7 +57,9 @@ object ExampleDatabase {
},
ExampleItem("Partial Image Loading") { PartialRequestFragment() },
ExampleItem("Platform Bitmap Factory") { ImagePipelineBitmapFactoryFragment() },
ExampleItem("Region Decoding") { ImagePipelineRegionDecodingFragment() })),
ExampleItem("Region Decoding") { ImagePipelineRegionDecodingFragment() },
),
),
ExampleCategory(
"Image Formats",
listOf(
Expand All @@ -67,7 +70,10 @@ object ExampleDatabase {
ExampleItem("SVG") { ImageFormatSvgFragment() },
ExampleItem("Keyframes") { ImageFormatKeyframesFragment() },
ExampleItem("Decoder Override") { ImageFormatOverrideExample() },
ExampleItem("Data URI") { ImageFormatDataUriFragment() })),
ExampleItem("Data URI") { ImageFormatDataUriFragment() },
ExampleItem("XML") { ImageFormatXmlFragment() },
),
),
ExampleCategory(
"Fresco Vito",
listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ShowcaseApplication : Application() {

imagePipelineConfigBuilder
.experiment()
.setBinaryXmlEnabled(true)
.setBitmapPrepareToDraw(true, 0, Integer.MAX_VALUE, true)
.setDownsampleIfLargeBitmap(true)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.fresco.samples.showcase.imageformat.xml;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.common.util.UriUtil;
import com.facebook.fresco.samples.showcase.BaseShowcaseFragment;
import com.facebook.fresco.samples.showcase.R;
import com.facebook.fresco.vito.options.ImageOptions;
import com.facebook.fresco.vito.view.VitoView;

/** This fragment displays different XML images. */
public class ImageFormatXmlFragment extends BaseShowcaseFragment {

private static final String CALLER_CONTEXT = "ImageFormatXmlFragment";

private ImageView mImageView;

@Nullable
@Override
public View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_format_xml, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mImageView = view.findViewById(R.id.image);

final Spinner spinner = view.findViewById(R.id.spinner);
final Entry[] entries =
new Entry[] {
new Entry("Vector", fromResourceId(R.drawable.xml_vector)),
new Entry("Nine patch", fromResourceId(R.drawable.xml_nine_patch)),
new Entry("Layer list", fromResourceId(R.drawable.xml_layer_list)),
new Entry("Level list", fromResourceId(R.drawable.xml_level_list)),
new Entry("State list", fromResourceId(R.drawable.xml_state_list)),
new Entry("Bitmap", fromResourceId(R.drawable.xml_bitmap)),
};
spinner.setAdapter(new SimpleUriListAdapter(entries));
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final Entry spinnerEntry = entries[spinner.getSelectedItemPosition()];
setImageUri(spinnerEntry.uri);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
spinner.setSelection(0);
}

private void setImageUri(Uri uri) {
VitoView.show(uri, ImageOptions.create().build(), CALLER_CONTEXT, mImageView);
}

private Uri fromResourceId(@DrawableRes int resourceId) {
return UriUtil.getUriForResourceId(resourceId);
}

private class SimpleUriListAdapter extends BaseAdapter {
final Entry[] mEntries;

SimpleUriListAdapter(Entry[] entries) {
this.mEntries = entries;
}

@Override
public int getCount() {
return mEntries.length;
}

@Override
public Entry getItem(int position) {
return mEntries[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater layoutInflater = getLayoutInflater(null);
final View view =
convertView != null
? convertView
: layoutInflater.inflate(
android.R.layout.simple_spinner_dropdown_item, parent, false);
final TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(mEntries[position].label);
return view;
}
}

private static class Entry {
final @NonNull String label;
final Uri uri;

private Entry(@NonNull String label, Uri uri) {
this.label = label;
this.uri = uri;
}
}
}
5 changes: 5 additions & 0 deletions samples/showcase/src/main/res/drawable/xml_bitmap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:src="@drawable/logo"
/>
24 changes: 24 additions & 0 deletions samples/showcase/src/main/res/drawable/xml_layer_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="@dimen/drawee_height_medium"
android:height="@dimen/drawee_height_medium"
android:drawable="@drawable/ic_error_black_96dp"
android:gravity="center"
/>

<item
android:width="@dimen/drawee_height_medium"
android:height="@dimen/drawee_height_medium"
android:drawable="@drawable/xml_vector"
android:gravity="center"
/>

<item
android:width="@dimen/drawee_height_medium"
android:height="@dimen/drawee_height_medium"
android:drawable="@drawable/ic_edit"
android:gravity="center"
/>

</layer-list>
12 changes: 12 additions & 0 deletions samples/showcase/src/main/res/drawable/xml_level_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_retry_black_48dp"
android:minLevel="1"
/>
<item
android:drawable="@drawable/xml_vector"
android:maxLevel="1"
android:minLevel="0"
/>
</level-list>
5 changes: 5 additions & 0 deletions samples/showcase/src/main/res/drawable/xml_nine_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="@drawable/ninepatch"
/>
5 changes: 5 additions & 0 deletions samples/showcase/src/main/res/drawable/xml_state_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_arrow_left" android:state_checked="false" android:state_pressed="false" />
<item android:drawable="@drawable/ic_arrow_right" android:state_checked="true" android:state_pressed="false" />
</selector>
13 changes: 13 additions & 0 deletions samples/showcase/src/main/res/drawable/xml_vector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#a4c639"
android:viewportWidth="24"
android:viewportHeight="24"
>
<path
android:fillColor="#000"
android:pathData="M17.6,11.48 L19.44,8.3a0.63,0.63 0,0 0,-1.09 -0.63l-1.88,3.24a11.43,11.43 0,0 0,-8.94 0L5.65,7.67a0.63,0.63 0,0 0,-1.09 0.63L6.4,11.48A10.81,10.81 0,0 0,1 20L23,20A10.81,10.81 0,0 0,17.6 11.48ZM7,17.25A1.25,1.25 0,1 1,8.25 16,1.25 1.25,0 0,1 7,17.25ZM17,17.25A1.25,1.25 0,1 1,18.25 16,1.25 1.25,0 0,1 17,17.25Z"
/>
</vector>
39 changes: 39 additions & 0 deletions samples/showcase/src/main/res/layout/fragment_format_xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".imageformat.xml.ImageFormatXmlFragment"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_medium"
/>

<ImageView
android:id="@+id/image"
android:layout_width="@dimen/drawee_width_medium"
android:layout_height="@dimen/drawee_height_medium"
android:layout_marginTop="@dimen/margin_medium"
android:importantForAccessibility="no"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_medium"
android:text="@string/format_xml_help"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
</ScrollView>
2 changes: 2 additions & 0 deletions samples/showcase/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@

<string name="format_datauri_help">Images can also be provided by data URIs such as the one below:</string>

<string name="format_xml_help" description="Helper text for xml example">This example showcases different binary XML file types. Select different binary XML types from the spinner.</string>

<string name="vito_litho_image_options_config_help_source">Configure the image source to be displayed</string>
<string name="vito_litho_image_options_config_help_options">Configure image options to change the image\'s appearance</string>
<string name="vito_litho_image_options_config_switch_auto_play_animations">Auto play animations</string>
Expand Down

0 comments on commit 23a16df

Please sign in to comment.