android 照片特效,Android使用Gallery实现照片拖动的特效

本文介绍了如何在Android中使用已废弃的Gallery控件实现照片拖动的特效。通过创建一个继承自BaseAdapter的自定义适配器,将图片资源绑定到Gallery控件上,并设置ImageView的缩放类型和布局参数来实现图片展示。虽然Gallery已被弃用,建议使用HorizontalScrollView或ViewPager代替,但此例子仍能帮助理解图片滑动效果的实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8af8eac2eda5aa3ed317b53350551da0.png

今天要分享一个非常简单的功能:

使用Android原生控件Gallery实现照片拖动的特效

实现思路如下:

在布局文件中定义一个Gallery控件

由于要显示多张图,为了方便,我直接引用了Android原生的图片资源

Gallery只是一个控件,为了将图片数据跟控件进行绑定,还需要一个继承BaseAdapter的自定义适配器

源码如下:

1、主activity和自定义内部类ImageAdapter:

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

import com.example.memorydemo.R;

public class SimpleGallery extends Activity {

private static final String TAG = "SimpleGallery";

@Override

protected void onCreate(Bundle onSavedInstance) {

super.onCreate(onSavedInstance);

setContentView(R.layout.simple_gallery_layout);

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

gallery.setAdapter(new ImageAdapter(this));

}

private class ImageAdapter extends BaseAdapter {

// 这里我们使用Android原生的资源图标

private int[] imageIds = {

android.R.drawable.btn_minus,

android.R.drawable.btn_radio,

android.R.drawable.ic_lock_idle_low_battery,

android.R.drawable.ic_menu_camera };

private Context mContext;

public ImageAdapter(Context context) {

mContext = context;

}

@Override

public int getCount() {

return imageIds.length;

}

@Override

public Object getItem(int position) {

return imageIds[position];

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ImageView imageView;

if (convertView == null) {

Log.i(TAG, "convertView is null, create new imageview");

imageView = new ImageView(mContext);

} else {

Log.i(TAG, "Cast convertView to ImageView");

imageView = (ImageView) convertView;

}

imageView.setImageResource(imageIds[position]);

imageView.setScaleType(ImageView.ScaleType.FIT_XY);

// 注意这里要用Gallery.LayoutParams作为布局参数类型,源码中给出了建议(Views given to the Gallery should use

// Gallery.LayoutParams s their ayout parameters type)

// 由于Android原生图片很小,我将高度设置为 500,方便看效果

imageView.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500));

return imageView;

}

}

}

2、布局文件 simple_gallery_layout.xml如下:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/gallery"

android:layout_width="match_parent"

android:layout_height="match_parent" />

注意:

Gallery控件其实已经被废弃了,建议用 HorizontalScrollView 和 ViewPager 代替,源码中是这么解释的:

@deprecated This widget is no longer supported. Other horizontally scrolling widgets include {@link HorizontalScrollView} and {@link android.support.v4.view.ViewPager} from the support library.

后续会分享 HorizontalScrollView 和 ViewPager这两个控件是如何使用的。

以上就是Android使用Gallery实现照片拖动的特效的详细内容,更多关于Android 照片拖动特效的资料请关注云海天教程其它相关文章!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值