Today I want to share a very simple feature:
Use Android native control Gallery to implement special effects of photo drag
The implementation idea is as follows:
- Define a Gallery control in the layout file
- Since I want to display multiple pictures, for convenience, I directly quoted the native image resources of Android
- Gallery is just a control. In order to bind image data to controls, a custom adapter that inherits BaseAdapter is also needed.
The source code is as follows:
1. Main activity and custom inner class ImageAdapter:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class SimpleGallery extends Activity { private static final String TAG = "SimpleGallery"; @Override protected void onCreate(Bundle onSavedInstance) { (onSavedInstance); setContentView(.simple_gallery_layout); Gallery gallery = findViewById(); (new ImageAdapter(this)); } private class ImageAdapter extends BaseAdapter { // Here we use Android native resource icon private int[] imageIds = { .btn_minus, .btn_radio, .ic_lock_idle_low_battery, .ic_menu_camera }; private Context mContext; public ImageAdapter(Context context) { mContext = context; } @Override public int getCount() { return ; } @Override public Object getItem(int position) { return imageIds[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { (TAG, "convertView is null, create new imageview"); imageView = new ImageView(mContext); } else { (TAG, "Cast convertView to ImageView"); imageView = (ImageView) convertView; } (imageIds[position]); (.FIT_XY); // Note that it should be used as the layout parameter type here. The source code gives (Views given to the Gallery should use // s their ayout parameters type) // Since the native Android image is very small, I set the height to 500 to facilitate the viewing of the effect (new (.MATCH_PARENT, 500)); return imageView; } } }
2. The layout file simple_gallery_layout.xml is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent"> <Gallery android: android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Notice:
The Gallery control has actually been abandoned. It is recommended to use HorizontalScrollView and ViewPager instead. The source code explains this:
@deprecated This widget is no longer supported. Other horizontally scrolling widgets include {@link HorizontalScrollView} and {@link .} from the support library.
We will share in the future how the two controls, HorizontalScrollView and ViewPager, are used.
The above is the detailed content of Android's special effects for using Gallery to implement photo dragging. For more information about Android's special effects for photo dragging, please follow my other related articles!