public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return imageInteger.length;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageInteger[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
return imageView;
}
}
// 循环Gallery的Adapter
class LoopImageAdapter extends BaseAdapter {
private Context context;
public LoopImageAdapter(Context context) {
this.context = context;
}
private Integer[] imageInteger = { R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9 };
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return Integer.MAX_VALUE;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageInteger[position
% imageInteger.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
return imageView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
main.xml
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:orientation=“vertical”
tools:context=“com.example.gallerydemo.Main” >
<TextView
android:id=“@+id/num”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@string/hello_world” />
<ImageView
android:layout_width=“50dp”
android:layout_height=“50dp”
android:layout_gravity=“center”
android:src=“@drawable/arrow_down” />
<Gallery
android:id=“@+id/gallery”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:gravity=“bottom” />
<Gallery
android:id=“@+id/galleryLoop”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:gravity=“bottom” />
<ImageView
android:layout_width=“50dp”
android:layout_height=“50dp”
android:layout_gravity=“center”
android:src=“@drawable/arrow_up” />
由于Gallery每次切换图片时都要新建视图,造成太多的资源浪费。所以已经弃用(Deprecated),可以用HorizontalScrollView来替代。今天对比以下传统Gallery和HorizontalScrollView的区别。看一下Demo的效果图:
另外也加入了带有阴影的3D Gallery,不过滑动效果不是很好。
以下是核心代码,全部代码可以到我的GitHub上查看。链接在文末。
package com.zms.simplegallery;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
public class Main extends ActionBarActivity {
/**
- 常规Gallery
*/
private Gallery galleryNormal;
/**
- 循环Gallery
*/
private Gallery galleryLoop;
/**
- 3D Gallery
*/
private Gallery3D gallery3D;
/**
- HorizontalScrollView Gallery
*/
private static final String tag = “GH”;
private static final String PATH_SDCARD = “/mnt/sdcard”;
private LinearLayout hsvGallery;
private LinearLayout hsvGalleryFromSD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
galleryNormal = (Gallery) findViewById(R.id.galleryNormal);
galleryNormal.setAdapter(new ImageAdapter(this));
galleryNormal.setSpacing(5); // 条目间距
galleryNormal.setOnItemClickListener(new MyItemOnClickListener());
galleryLoop = (Gallery) findViewById(R.id.galleryLoop);
galleryLoop.setAdapter(new LoopImageAdapter(this));
galleryLoop.setSpacing(5);
galleryLoop.setOnItemClickListener(new MyItemOnClickListener());
Integer[] images = {R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8};
Gallery3DAdapter adapter = new Gallery3DAdapter(this, images);
adapter.createReflectedImages(); // 创建倒影效果
gallery3D = (Gallery3D) this.findViewById(R.id.gallery3d);
gallery3D.setFadingEdgeLength(0);
gallery3D.setSpacing(-100);
gallery3D.setAdapter(adapter);
gallery3D.setOnItemClickListener(new MyItemOnClickListener());
gallery3D.setSelection(4);
hsvGallery = (LinearLayout) findViewById(R.id.hsvGallery);
hsvGalleryFromSD = (LinearLayout) findViewById(R.id.hsvGalleryFromSD);
//pic in the drawable
Integer[] images2 = {R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9};
for (Integer id : images2) {
hsvGallery.addView(insertImage(id));
}
//pic in the sdcard
String targetPath = PATH_SDCARD + “/DCIM/Camera/”;
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
if (files != null) {
Log.d(tag, files.length + “”);
for (File file : files) {
hsvGalleryFromSD.addView(insertPhoto(file.getAbsolutePath()));
}
}
}
private class MyItemOnClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView tvHint = (TextView) findViewById(R.id.tvHint);
if (view == galleryNormal) {
int num = position + 1;
tvHint.setText(“常规Gallery:点击了” + num);
} else if (view == galleryLoop) {
int num = (position + 1) % 9;
if (num == 0) {
num = 9;
}
tvHint.setText(“循环Gallery:点击了” + num);
} else if (view == gallery3D) {
int num = position + 1;
tvHint.setText(“3D Gallery:点击了” + num);
}
}
}
// 普通Gallery的Adapter
class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
private Integer[] imageInteger = {R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9};
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// 常规Gallery和循环Gallery,此函数返回值不同
public int getCount() {
return imageInteger.length;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageInteger[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY); // 显示比例类型
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100)); //图片大小
return imageView;
}
}
// 循环Gallery的Adapter
class LoopImageAdapter extends BaseAdapter {
private Context context;
public LoopImageAdapter(Context context) {
this.context = context;
}
private Integer[] imageInteger = {R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9};
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return Integer.MAX_VALUE;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageInteger[position % imageInteger.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
return imageView;
}
}
private View insertImage(Integer id) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(320, 320));
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(300, 300));
imageView.setBackgroundResource(id);
layout.addView(imageView);
return layout;
}
private View insertPhoto(String absolutePath) {
// TODO Auto-generated method stub
Bitmap bm = decodeSampleBitmapFromUri(absolutePath, 200, 200);
LinearLayout layout = new LinearLayout(getApplicationContext());
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
尾声
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。
- 思维脑图
- 性能优化学习笔记
- 性能优化视频
当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
声
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。
- 思维脑图
[外链图片转存中…(img-sdcTsaU5-1713759262329)] - 性能优化学习笔记
[外链图片转存中…(img-RqF8Orpt-1713759262330)]
[外链图片转存中…(img-R0alWuLN-1713759262331)]
[外链图片转存中…(img-JEpm8ikP-1713759262332)]
[外链图片转存中…(img-B2QwSCNp-1713759262333)]
- 性能优化视频
[外链图片转存中…(img-h0kSLz1n-1713759262333)]
当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!