当前位置:网站首页>Use image components to slide through photo albums and mobile phone photo album pages
Use image components to slide through photo albums and mobile phone photo album pages
2022-07-06 02:05:00 【Standing on the shoulders of giants, review the past and know t】
The image component contains image views (ImageView), Image switcher (ImageSwitcher) And grid view (GridView).
chart 1 Image component inheritance diagram
As can be seen from the above figure ,ImageView Inherited from View, For rendering images ;ImageSwitcher Inherited from FrameLayout, So you can achieve animation effect ;GridView Inherited from AdapterView,AdapterView Inherited from ViewGroup(View Containers ), So it can contain multiple list items .
1 Image view
Image view (ImageView) Used to display any Drawable object .
<ImageView> The tag syntax format is as follows :
<ImageView
Property list
>
</ImageView>
ImageView Supported by xml The attributes are shown in the table 1:
xml attribute | describe |
andriod:adjustViewBounds | Set up ImageView Whether to adjust the border to maintain the aspect ratio of the displayed image |
android:maxHeight | Set up ImageView The maximum height of , Premise andriod:adjustViewBounds by true |
android:maxWidth | Set up ImageView The maximum width of , Premise andriod:adjustViewBounds by true |
android:scaleType | Set how the displayed image zooms or moves to fit ImageView Size |
android:src | Set up ImageView What is shown Drawable Object's ID |
android:tint | For coloring pictures |
surface 1 ImageView Supported by xml attribute
2 Image switcher
Image switcher (ImageSwitcher) It is used to realize the picture switching function with animation effect .
Common interfaces :
setFactory(): by ImageSwitcher Class to set a ViewFactory, Used to separate the displayed picture from the parent window .
setImageResource(): Used to specify to be in ImageSwitcher Picture resources shown in .
ImageSwitcher: Realize sliding View album
MainActivity.java
public class MainActivity extends Activity {
private int[] arrayPictures = new int[]{R.mipmap.img01, R.mipmap.img02, R.mipmap.img03,
R.mipmap.img04, R.mipmap.img05, R.mipmap.img06,
R.mipmap.img07, R.mipmap.img08, R.mipmap.img09,
};
private ImageSwitcher imageSwitcher;
private int pictutureIndex;
// Finger pressed X coordinate
private float touchDownX;
// Fingers loose X coordinate
private float touchUpX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(arrayPictures[pictutureIndex]);
return imageView;
}
});
imageSwitcher.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Get finger pressed X coordinate
touchDownX = event.getX();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// Get fingers loose X coordinate
touchUpX = event.getX();
if (touchUpX - touchDownX > 100) { // From left to right , Look at the next one
pictutureIndex = pictutureIndex == 0 ? arrayPictures.length - 1 : pictutureIndex - 1;
// Animate picture switching
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_out_right));
// Set the current picture to see
imageSwitcher.setImageResource(arrayPictures[pictutureIndex]);
} else if (touchDownX - touchUpX > 100) // From right to left , Look at the last one
{
pictutureIndex = pictutureIndex == arrayPictures.length - 1 ? 0 : pictutureIndex + 1;
// Animate the switch
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_out_left));
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in_right));
// Set the picture you want to see
imageSwitcher.setImageResource(arrayPictures[pictutureIndex]);
}
return true;
}
return false;
}
});
}
}
https://github.com/hanyuhang-hz/android-demos
3 Grid view
Grid view (GridView) According to the line , Column distribution to display multiple components .
<GridView> The basic syntax is as follows :
<GridView
Property list
>
</GridView>
GridView Components support xml The attributes are shown in the table 2:
xml attribute | describe |
android:columnWidth | Set the width of the column |
android:gravity | Set alignment |
android:stretchMode | Set stretch mode |
android:numColumns | Set number of columns |
android:horizontalSpacing | Set the horizontal spacing of each element |
android:verticalSpacing | Set the vertical spacing of each element |
surface 2 GridView Components support xml attribute
Adapter Class is an interface , It is the bridge between data and components , It can be used to GridView Provide data . Common implementation classes are as follows :
(1)ArrayAdapter: Array adapter , Show a line of text .
(2)SimpleAdapter: Simple adapter , Usually used to apply List Multiple values of are wrapped into list items .
(3)SimpleCursorAdapter: The content of the database is displayed in the form of a list .
(4)BaseAdapter: abstract class , It can customize each list item to the greatest extent .
GridView: adopt BaseAdapter The adapter specifies how the content is created GridView, Realize the mobile photo album page
MainActivity.java
public class MainActivity extends Activity {
private Integer[] picture = {R.mipmap.img01, R.mipmap.img02, R.mipmap.img03,
R.mipmap.img04, R.mipmap.img05, R.mipmap.img06, R.mipmap.img07,
R.mipmap.img08, R.mipmap.img09, R.mipmap.img10, R.mipmap.img11,
R.mipmap.img12,};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView= (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c){
mContext=c;
}
@Override
public int getCount() {
return picture.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(mContext);
// Set the width and height for the component
imageView.setLayoutParams(new GridView.LayoutParams(300, 176));
// Select the picture laying method
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}else{
imageView= (ImageView) convertView;
}
imageView.setImageResource(picture[position]);
return imageView;
}
}
}
边栏推荐
- FTP server, ssh server (super brief)
- Xshell 7 Student Edition
- Force buckle 9 palindromes
- How to set an alias inside a bash shell script so that is it visible from the outside?
- Reasonable and sensible
- Online reservation system of sports venues based on PHP
- How does redis implement multiple zones?
- [depth first search] Ji Suan Ke: Betsy's trip
- 插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
- Text editing VIM operation, file upload
猜你喜欢
PHP campus movie website system for computer graduation design
使用npm发布自己开发的工具包笔记
Online reservation system of sports venues based on PHP
[flask] official tutorial -part2: Blueprint - view, template, static file
Redis string type
dried food! Accelerating sparse neural network through hardware and software co design
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
Leetcode skimming questions_ Verify palindrome string II
How to improve the level of pinduoduo store? Dianyingtong came to tell you
Social networking website for college students based on computer graduation design PHP
随机推荐
Ali test Open face test
Computer graduation design PHP enterprise staff training management system
500 lines of code to understand the principle of mecached cache client driver
FTP server, ssh server (super brief)
[flask] obtain request information, redirect and error handling
Leetcode3. Implement strstr()
How to upgrade kubernetes in place
Luo Gu P1170 Bugs Bunny and Hunter
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
A basic lintcode MySQL database problem
Dynamics 365 开发协作最佳实践思考
Kubernetes stateless application expansion and contraction capacity
Accelerating spark data access with alluxio in kubernetes
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
Jisuanke - t2063_ Missile interception
Leetcode skimming questions_ Sum of squares
Grabbing and sorting out external articles -- status bar [4]
GBase 8c数据库升级报错
Redis string type
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower