亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用Gallery實現(xiàn)照片拖動的特效

 更新時間:2021年01月22日 10:34:08   作者:snowyeti  
這篇文章主要介紹了Android如何使用Gallery實現(xiàn)照片拖動的特效,幫助大家更好的理解和利用Android進行開發(fā),感興趣的朋友可以了解下

今天要分享一個非常簡單的功能:

使用Android原生控件Gallery實現(xiàn)照片拖動的特效

實現(xiàn)思路如下:

  1. 在布局文件中定義一個Gallery控件
  2. 由于要顯示多張圖,為了方便,我直接引用了Android原生的圖片資源
  3. Gallery只是一個控件,為了將圖片數(shù)據(jù)跟控件進行綁定,還需要一個繼承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原生的資源圖標
    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控件其實已經(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實現(xiàn)照片拖動的特效的詳細內(nèi)容,更多關(guān)于Android 照片拖動特效的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android獲取SD卡上圖片和視頻縮略圖的小例子

    Android獲取SD卡上圖片和視頻縮略圖的小例子

    如果我們需要快速提取圖片和視頻縮略圖可以直接訪問android.provider.MediaStore.Images.Thumbnails 和android.provider.MediaStore.Video.Thumbnails這兩個數(shù)據(jù)庫,即可查詢出來縮略圖 。
    2013-06-06
  • 淺談Android應(yīng)用安全防護和逆向分析之a(chǎn)pk反編譯

    淺談Android應(yīng)用安全防護和逆向分析之a(chǎn)pk反編譯

    我們有時候在某個app上見到某個功能,某個效果蠻不錯的,我們想看看對方的思路怎么走的,這時候,我們就可以通過反編譯來編譯該apk,拿到代碼,進行分析。
    2021-06-06
  • Android仿微信語音聊天功能

    Android仿微信語音聊天功能

    這篇文章主要介紹了Android仿微信語音聊天功能,很實用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android設(shè)備上非root的抓包實現(xiàn)方法(Tcpdump方法)

    Android設(shè)備上非root的抓包實現(xiàn)方法(Tcpdump方法)

    通常我們在Android應(yīng)用中執(zhí)行某個命令時會使用“Runtime.getRuntime().exec("命令路徑")”這種方式,但是當(dāng)我們執(zhí)行抓包操作時,使用這條命令無論如何都不行,通過下面代碼打印結(jié)果發(fā)現(xiàn),該命令一定要在root權(quán)限下才能執(zhí)行,具體實現(xiàn)思路,請參考本教程
    2016-11-11
  • Android基礎(chǔ)之Activity生命周期

    Android基礎(chǔ)之Activity生命周期

    activity類是Android 應(yīng)用生命周期的重要部分。在系統(tǒng)中的Activity被一個Activity棧所管理。當(dāng)一個新的Activity啟動時,將被放置到棧頂,成為運行中的Activity,前一個Activity保留在棧中,不再放到前臺,直到新的Activity退出為止。
    2016-05-05
  • 內(nèi)存泄露導(dǎo)致Android?中setVisibility()?失效原理

    內(nèi)存泄露導(dǎo)致Android?中setVisibility()?失效原理

    這篇文章主要介紹了內(nèi)存泄露導(dǎo)致Android?中setVisibility()?失效原理,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-07-07
  • 詳解Android?Flutter如何使用相機實現(xiàn)拍攝照片

    詳解Android?Flutter如何使用相機實現(xiàn)拍攝照片

    在app中使用相機肯定是再平常不過的一項事情了,相機肯定涉及到了底層原生代碼的調(diào)用,那么在flutter中如何快速簡單的使用上相機的功能呢?一起來看看吧
    2023-04-04
  • android端微信支付V3版本地簽名統(tǒng)一下單詳解

    android端微信支付V3版本地簽名統(tǒng)一下單詳解

    本篇文章主要介紹了android端微信支付V3版本地簽名統(tǒng)一下單,具有一定的參考價值,有興趣的同學(xué)可以了解一下。
    2016-11-11
  • Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法

    Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法

    這篇文章主要介紹了Android開發(fā)實現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法,結(jié)合實例形式分析了Launcher3相關(guān)配置文件與功能函數(shù)修改設(shè)置操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android學(xué)習(xí)筆記之ContentProvider和Uri詳解

    Android學(xué)習(xí)筆記之ContentProvider和Uri詳解

    本篇文章主要介紹了Android學(xué)習(xí)筆記之ContentProvider和Uri詳解,對于學(xué)習(xí)Android的朋友具有一定的參考價值,有需要可以可以了解一下。
    2016-11-11

最新評論