当前位置:网站首页>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;
}
}
}
边栏推荐
- 02.Go语言开发环境配置
- LeetCode 322. Change exchange (dynamic planning)
- Regular expressions: examples (1)
- Open source | Ctrip ticket BDD UI testing framework flybirds
- Luo Gu P1170 Bugs Bunny and Hunter
- Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
- Computer graduation design PHP animation information website
- 【网络攻防实训习题】
- [understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
- Leetcode skimming questions_ Invert vowels in a string
猜你喜欢
Numpy array index slice
Computer graduation design PHP enterprise staff training management system
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
Basic operations of database and table ----- set the fields of the table to be automatically added
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
【clickhouse】ClickHouse Practice in EOI
干货!通过软硬件协同设计加速稀疏神经网络
2022 PMP project management examination agile knowledge points (8)
随机推荐
D22:indeterminate equation (indefinite equation, translation + problem solution)
Ali test Open face test
Social networking website for college students based on computer graduation design PHP
Luo Gu P1170 Bugs Bunny and Hunter
[depth first search] Ji Suan Ke: Betsy's trip
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
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
Executing two identical SQL statements in the same sqlsession will result in different total numbers
genius-storage使用文档,一个浏览器缓存工具
SPI communication protocol
Competition question 2022-6-26
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
竞价推广流程
Redis list
剑指 Offer 38. 字符串的排列
剑指 Offer 12. 矩阵中的路径
Leetcode skimming questions_ Invert vowels in a string
安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3
Xshell 7 Student Edition
Selenium element positioning (2)