Android使用Gallery實(shí)現(xiàn)照片拖動的特效
今天要分享一個非常簡單的功能:
使用Android原生控件Gallery實(shí)現(xiàn)照片拖動的特效
實(shí)現(xiàn)思路如下:
- 在布局文件中定義一個Gallery控件
- 由于要顯示多張圖,為了方便,我直接引用了Android原生的圖片資源
- Gallery只是一個控件,為了將圖片數(shù)據(jù)跟控件進(jìn)行綁定,還需要一個繼承BaseAdapter的自定義適配器
源碼如下:
1、主activity和自定義內(nèi)部類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原生的資源圖標(biāo)
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作為布局參數(shù)類型,源碼中給出了建議(Views given to the Gallery should use
// Gallery.LayoutParams s their ayout parameters type)
// 由于Android原生圖片很小,我將高度設(shè)置為 500,方便看效果
imageView.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500));
return imageView;
}
}
}
2、布局文件 simple_gallery_layout.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
注意:
Gallery控件其實(shí)已經(jīng)被廢棄了,建議用 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.
后續(xù)會分享 HorizontalScrollView 和 ViewPager這兩個控件是如何使用的。
以上就是Android使用Gallery實(shí)現(xiàn)照片拖動的特效的詳細(xì)內(nèi)容,更多關(guān)于Android 照片拖動特效的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談Android應(yīng)用安全防護(hù)和逆向分析之a(chǎn)pk反編譯
我們有時候在某個app上見到某個功能,某個效果蠻不錯的,我們想看看對方的思路怎么走的,這時候,我們就可以通過反編譯來編譯該apk,拿到代碼,進(jìn)行分析。2021-06-06
Android設(shè)備上非root的抓包實(shí)現(xiàn)方法(Tcpdump方法)
通常我們在Android應(yīng)用中執(zhí)行某個命令時會使用“Runtime.getRuntime().exec("命令路徑")”這種方式,但是當(dāng)我們執(zhí)行抓包操作時,使用這條命令無論如何都不行,通過下面代碼打印結(jié)果發(fā)現(xiàn),該命令一定要在root權(quán)限下才能執(zhí)行,具體實(shí)現(xiàn)思路,請參考本教程2016-11-11
內(nèi)存泄露導(dǎo)致Android?中setVisibility()?失效原理
這篇文章主要介紹了內(nèi)存泄露導(dǎo)致Android?中setVisibility()?失效原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-07-07
詳解Android?Flutter如何使用相機(jī)實(shí)現(xiàn)拍攝照片
在app中使用相機(jī)肯定是再平常不過的一項(xiàng)事情了,相機(jī)肯定涉及到了底層原生代碼的調(diào)用,那么在flutter中如何快速簡單的使用上相機(jī)的功能呢?一起來看看吧2023-04-04
android端微信支付V3版本地簽名統(tǒng)一下單詳解
本篇文章主要介紹了android端微信支付V3版本地簽名統(tǒng)一下單,具有一定的參考價值,有興趣的同學(xué)可以了解一下。2016-11-11
Android開發(fā)實(shí)現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法,結(jié)合實(shí)例形式分析了Launcher3相關(guān)配置文件與功能函數(shù)修改設(shè)置操作技巧,需要的朋友可以參考下2017-11-11
Android學(xué)習(xí)筆記之ContentProvider和Uri詳解
本篇文章主要介紹了Android學(xué)習(xí)筆記之ContentProvider和Uri詳解,對于學(xué)習(xí)Android的朋友具有一定的參考價值,有需要可以可以了解一下。2016-11-11

