Android控件开发之Gallery3D酷炫效果(带源码)

public class MainActivity extends Activity {

DisplayImageOptions options;

private ImageLoader imageLoader;

private FancyCoverFlow fancyCoverFlow;

private List filmList;

private ImageAdapter adapter;

private int cur_index = 0;

private int count_drawble;

private static int MSG_UPDATE = 1;

// 定时任务

private ScheduledExecutorService scheduledExecutorService;

// 通过handler来更新主界面

private Handler handler = new Handler() {

public void handleMessage(Message msg) {

if (msg.what == MSG_UPDATE) {

fancyCoverFlow.setSelection(cur_index);

}

}

};

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

filmList = FilmInfoTest.getfilmInfo();

Log.i(“INFO”, filmList.size()+“条目数”);

// 配置option

options = new DisplayImageOptions.Builder()

.showStubImage(R.drawable.logo)

.showImageForEmptyUri(R.drawable.logo)

.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)

.cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();

imageLoader = ImageLoader.getInstance();

adapter = new ImageAdapter(this, filmList, options, imageLoader);

fancyCoverFlow = (FancyCoverFlow) findViewById(R.id.fancyCoverFlow);

// item之间的间隙可以近似认为是imageview的宽度与缩放比例的乘积的一半

fancyCoverFlow.setSpacing(-180);

fancyCoverFlow.setAdapter(adapter);

fancyCoverFlow.setSelection(1002);

// fancyCoverFlow.setActionDistance(10);

fancyCoverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView<?> parent, View view,

int position, long id) {

cur_index = position;

}

@Override

public void onNothingSelected(AdapterView<?> parent) {

}

});

// 点击事件

fancyCoverFlow.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this,

filmList.get(position % filmList.size()).getFilmName(),

0).show();

}

});

// // 开启自动轮播

// count_drawble = adapter.getCount();

// startPlay();

}

/**

  • 开始轮播图切换

*/

private void startPlay() {

scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

scheduledExecutorService.scheduleAtFixedRate(new AutoPlayTask(), 1, 4,

TimeUnit.SECONDS);

}

/**

  • 停止轮播图切换

*/

private void stopPlay() {

scheduledExecutorService.shutdown();

}

/**

  • 执行轮播图切换任务

*/

private class AutoPlayTask implements Runnable {

@Override

public void run() {

// cur_index = cur_index % count_drawble; // 图片区间[0,count_drawable)

// Message msg = handler.obtainMessage(MSG_UPDATE);

// handler.sendMessage(msg);

// cur_index++;

}

}

@Override

protected void onStop() {

imageLoader.stop();

super.onStop();

}

二:适配器

这里我主要是用本地资源进行测试了一下,设置了倒影等等,当然,你也可以改为xml布局设置图片的

public class ImageAdapter extends FancyCoverFlowAdapter {

private Context context;

private List filmList;

// private ImageLoader imageLoader;

// private DisplayImageOptions options;

public ImageAdapter(Context context, List filmList,

DisplayImageOptions options, ImageLoader imageLoader) {

this.context = context;

this.filmList = filmList;

// this.options = options;

// this.imageLoader = imageLoader;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return Integer.MAX_VALUE;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return filmList.get(position);

// return position;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position % filmList.size();

// return position;

}

@Override

public View getCoverFlowItem(int position, View reusableView,

ViewGroup parent) {

ImageView imageView = (ImageView) reusableView;

if (imageView == null) {

imageView = new ImageView(context);

}

Resources re = context.getResources();

InputStream is = re.openRawResource(filmList.get(position%filmList.size()).getRs());

// InputStream is = re.openRawResource(mImagesId[position%mImagesId.length]);

BitmapDrawable mapdraw = new BitmapDrawable(is);

Bitmap bitmap = mapdraw.getBitmap();

imageView.setImageBitmap(BitmapUtil.createReflectedBitmap(bitmap));

// imageView.setImageBitmap(bitmap);

// ps.电影海报宽高比例一般为3:4

imageView.setLayoutParams(new Gallery.LayoutParams(410, 713));

// // 异步加载图片

// imageLoader.displayImage(filmList.get(position % filmList.size())

// .getFilmImageLink(), imageView, options);

imageView.setScaleType(ScaleType.CENTER_CROP);

return imageView;

}

public Integer[] getImagesId(){

return mImagesId;

}

public void setImagesId(Integer[] mImagesId){

this.mImagesId = mImagesId;

}

private Integer mImagesId[] = {

R.drawable.ic_1,

R.drawable.ic_3,

R.drawable.ic_2,

R.drawable.ic_4,

R.drawable.ic_5

};

三:gallery 控件类

缩放,还有透明,等等都在这里设置

public class FancyCoverFlow extends Gallery {

public static final int ACTION_DISTANCE_AUTO = Integer.MAX_VALUE;

/**

  • 图片向上突出,可以通过代码控制,也可以在xml上控制

*/

public static final float SCALEDOWN_GRAVITY_TOP = 0.0f;

/**

  • 图片中间突出

*/

public static final float SCALEDOWN_GRAVITY_CENTER = 0.5f;

/**

  • 图片向下突出

*/

public static final float SCALEDOWN_GRAVITY_BOTTOM = 1.0f;

private float reflectionRatio = 0.3f;

private int reflectionGap = 4;

private boolean reflectionEnabled = false;

private float unselectedAlpha;

private Camera transformationCamera;

private int maxRotation = 0;

private float unselectedScale;

private float scaleDownGravity = SCALEDOWN_GRAVITY_CENTER;

private int actionDistance;

private float unselectedSaturation;

public FancyCoverFlow(Context context) {

super(context);

this.initialize();

}

public FancyCoverFlow(Context context, AttributeSet attrs) {

super(context, attrs);

this.initialize();

this.applyXmlAttributes(attrs);

}

@SuppressLint(“NewApi”)

public FancyCoverFlow(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

if (Build.VERSION.SDK_INT >= 11) {

this.setLayerType(LAYER_TYPE_SOFTWARE, null);

}

this.initialize();

this.applyXmlAttributes(attrs);

}

private void initialize() {

this.transformationCamera = new Camera();

this.setSpacing(0);

}

private void applyXmlAttributes(AttributeSet attrs) {

TypedArray a = getContext().obtainStyledAttributes(attrs,

R.styleable.FancyCoverFlow);

this.actionDistance = a

.getInteger(R.styleable.FancyCoverFlow_actionDistance,

ACTION_DISTANCE_AUTO);

this.scaleDownGravity = a.getFloat(

R.styleable.FancyCoverFlow_scaleDownGravity, 0.5f);

this.maxRotation = a.getInteger(R.styleable.FancyCoverFlow_maxRotation,

0);

this.unselectedAlpha = a.getFloat(

R.styleable.FancyCoverFlow_unselectedAlpha, 0.5f);

this.unselectedSaturation = a.getFloat(

R.styleable.FancyCoverFlow_unselectedSaturation, 0.0f);

this.unselectedScale = a.getFloat(

R.styleable.FancyCoverFlow_unselectedScale, 0.75f);

}

public float getReflectionRatio() {

return reflectionRatio;

}

public void setReflectionRatio(float reflectionRatio) {

if (reflectionRatio <= 0 || reflectionRatio > 0.5f) {

throw new IllegalArgumentException(

“reflectionRatio may only be in the interval (0, 0.5]”);

}

this.reflectionRatio = reflectionRatio;

if (this.getAdapter() != null) {

((FancyCoverFlowAdapter) this.getAdapter()).notifyDataSetChanged();

}

}

public int getReflectionGap() {

return reflectionGap;

}

public void setReflectionGap(int reflectionGap) {

this.reflectionGap = reflectionGap;

if (this.getAdapter() != null) {

((FancyCoverFlowAdapter) this.getAdapter()).notifyDataSetChanged();

}

}

public boolean isReflectionEnabled() {

return reflectionEnabled;

}

public void setReflectionEnabled(boolean reflectionEnabled) {

this.reflectionEnabled = reflectionEnabled;

if (this.getAdapter() != null) {

((FancyCoverFlowAdapter) this.getAdapter()).notifyDataSetChanged();

}

}

@Override

public void setAdapter(SpinnerAdapter adapter) {

if (!(adapter instanceof FancyCoverFlowAdapter)) {

throw new ClassCastException(FancyCoverFlow.class.getSimpleName()

  • " only works in conjunction with a "

  • FancyCoverFlowAdapter.class.getSimpleName());

}

super.setAdapter(adapter);

}

public int getMaxRotation() {

return maxRotation;

}

public void setMaxRotation(int maxRotation) {

this.maxRotation = maxRotation;

}

public float getUnselectedAlpha() {

return this.unselectedAlpha;

}

public float getUnselectedScale() {

return unselectedScale;

}

public void setUnselectedScale(float unselectedScale) {

this.unselectedScale = unselectedScale;

}

public float getScaleDownGravity() {

return scaleDownGravity;

}

public void setScaleDownGravity(float scaleDownGravity) {

this.scaleDownGravity = scaleDownGravity;

}

public int getActionDistance() {

return actionDistance;

}

public void setActionDistance(int actionDistance) {

this.actionDistance = actionDistance;

}

@Override

public void setUnselectedAlpha(float unselectedAlpha) {

super.setUnselectedAlpha(unselectedAlpha);

this.unselectedAlpha = unselectedAlpha;

}

public float getUnselectedSaturation() {

return unselectedSaturation;

}

public void setUnselectedSaturation(float unselectedSaturation) {

this.unselectedSaturation = unselectedSaturation;

}

public int preLeftOffset = 0;

public int count = 0;

public boolean isPlayDraw = true;

@Override

protected boolean getChildStaticTransformation(View child, Transformation t) {

FancyCoverFlowItemWrapper item = (FancyCoverFlowItemWrapper) child;

preLeftOffset = getChildAt(0).getLeft();

if (android.os.Build.VERSION.SDK_INT >= 16) {

item.postInvalidate();

}

final int coverFlowWidth = this.getWidth();

final int coverFlowCenter = coverFlowWidth / 2;

final int childWidth = item.getWidth();

final int childHeight = item.getHeight();

final int childCenter = item.getLeft() + childWidth / 2;

final int actionDistance = (this.actionDistance == ACTION_DISTANCE_AUTO) ? (int) ((coverFlowWidth + childWidth) / 2.0f)
this.actionDistance;

float effectsAmount = Math.min(

1.0f,

Math.max(-1.0f, (1.0f / actionDistance)

  • (childCenter - coverFlowCenter)));

t.clear();

t.setTransformationType(Transformation.TYPE_BOTH);

if (this.unselectedAlpha != 1) {

final float alphaAmount = (this.unselectedAlpha - 1)

  • Math.abs(effectsAmount) + 1;

t.setAlpha(alphaAmount);

}

if (this.unselectedSaturation != 1) {

// Pass over saturation to the wrapper.

final float saturationAmount = (this.unselectedSaturation - 1)

  • Math.abs(effectsAmount) + 1;

item.setSaturation(saturationAmount);

}

final Matrix imageMatrix = t.getMatrix();

// 旋转角度不为0则开始图片旋转.

if (this.maxRotation != 0) {

final int rotationAngle = (int) (-effectsAmount * this.maxRotation);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

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

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

七大模块学习资料:如NDK模块开发、Android框架体系架构…

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。

由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

…(img-OPEOJOxQ-1713264718543)]

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

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

[外链图片转存中…(img-QTPuytxv-1713264718544)]

七大模块学习资料:如NDK模块开发、Android框架体系架构…

[外链图片转存中…(img-i2b16K6G-1713264718545)]

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。

由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值