Android 循环Gallery,3D Gallery以及HorizontalScrollView替代Gallery

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注Android)
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

正文

private Gallery gallery = null;

private Gallery galleryLoop = null;

private TextView tvNum = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tvNum = (TextView) findViewById(R.id.num);

gallery = (Gallery) findViewById(R.id.gallery);

gallery.setAdapter(new ImageAdapter(this));

gallery.setSpacing(5);

gallery.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

int num = arg2 + 1;

// Toast.makeText(Main.this, “点击了” + num, Toast.LENGTH_SHORT)

// .show();

tvNum.setText(“常规Gallery:点击了” + num);

}

});

galleryLoop = (Gallery) findViewById(R.id.galleryLoop);

galleryLoop.setAdapter(new LoopImageAdapter(this));

galleryLoop.setSpacing(10);

galleryLoop.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

int num = (arg2 + 1) % 9;

if (num == 0) {

num = 9;

}

// Toast.makeText(Main.this, “点击了” + num, Toast.LENGTH_SHORT)

// .show();

tvNum.setText(“循环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;

}

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;

总结

首先是感觉自己的基础还是不够吧,大厂好像都喜欢问这些底层原理。

另外一部分原因在于资料也还没有看完,一面时凭借那份资料考前突击恶补个几天居然也能轻松应对(在这里还是要感谢那份资料,真的牛),于是自我感觉良好,资料就没有怎么深究下去了。

之前的准备只涉及了Java、Android、计网、数据结构与算法这些方面,面对面试官对其他基础课程的考察显得捉襟见肘。

下一步还是要查漏补缺,进行针对性复习。

最后的最后,那套资料这次一定要全部看完,是真的太全面了,各个知识点都涵盖了,几乎我面试遇到的所有问题的知识点这里面都有!希望大家不要犯和我一样的错误呀!!!一定要看完!
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

另外一部分原因在于资料也还没有看完,一面时凭借那份资料考前突击恶补个几天居然也能轻松应对(在这里还是要感谢那份资料,真的牛),于是自我感觉良好,资料就没有怎么深究下去了。

之前的准备只涉及了Java、Android、计网、数据结构与算法这些方面,面对面试官对其他基础课程的考察显得捉襟见肘。

下一步还是要查漏补缺,进行针对性复习。

最后的最后,那套资料这次一定要全部看完,是真的太全面了,各个知识点都涵盖了,几乎我面试遇到的所有问题的知识点这里面都有!希望大家不要犯和我一样的错误呀!!!一定要看完!
[外链图片转存中…(img-2DmGq8Ns-1713678145784)]

[外链图片转存中…(img-gp6EGJae-1713678145784)]

[外链图片转存中…(img-oU6BaNjo-1713678145785)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-PC15lWEn-1713678145785)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值