這次的團隊項目我是被分到Android項目開發組的,所以這篇我的技術博客主要是有關在Android開發過程當中遇到的難題以及一些學到的一些相關的技術。
***java
安裝Android Studio的時候基本就是參照網上的教程啦作的,但有一個地方卡了我好久,就是程序一直卡在"Building Gradle project info"不動,分析應該是你懂得的網絡問題,網上看了挺多解決辦法,但都沒有解決。。。。。最後是看到CSDN上一個的博客:打開gradle-wrapper.properties,複製裏面的連接地址用迅雷下載獲得"gradle-4.1-all.zip"而後放到指定的文件夾下。問題完美解決。
***android
package com.example.jorgezhu.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.*; import android.view.View.OnClickListener; import android.view.*; public class MainActivity extends AppCompatActivity { private TextView textView; private Button button; private int sum=0; private ButtonListener buttonListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView=(TextView)findViewById(R.id.textView01); textView.setText("google is a great"); button=(Button)findViewById(R.id.button01); buttonListener=new ButtonListener(); button.setOnClickListener(buttonListener); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v){ sum++; textView.setText("當前點擊次數爲"+sum); } } }
我作的模塊須要讀取相應目錄下的圖片,但不知道爲何一直讀取不成功,後面發現好像是權限問題,百度了一夜的如何獲取讀取權限都沒效果,百度上基本上都是叫我在AndroidManifest裏面加
看了網上的相關教程,今天終於把StackView圖片瀏覽功能作出來了。首先定義一個主activity:PicStackViewActivity,相關代碼以下:github
package com.example.mcjproject; import android.Manifest; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.StackView; import android.widget.TextView; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Timer; public class PicStackViewActivity extends AppCompatActivity { private StackView stackView; private List<String> images = new ArrayList<>(); private ImageAdapter imageAdapter; private TextView textView; private Timer down; private Timer timerup; private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pic_view); stackView = (StackView) findViewById(R.id.stackview); textView = (TextView) findViewById(R.id.textview); ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},140); images=getPictures(Environment.getExternalStorageDirectory()+"/Download"); imageAdapter = new ImageAdapter(images, this); stackView.setAdapter(imageAdapter); stackView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { textView.setText("第"+(position+1)+"個楊冪"); } }); } public List<String> getPictures(final String strPath) { List<String> list = new ArrayList<String>(); File file = new File(strPath); /*if(file.canRead()) { Log.d("hello","canread"); }*/ File[] allfiles = file.listFiles(); if (allfiles == null) { return null; } for(int k = 0; k < allfiles.length; k++) { final File fi = allfiles[k]; if(fi.isFile()) { int idx = fi.getPath().lastIndexOf("."); if (idx <= 0) { continue; } String suffix = fi.getPath().substring(idx); if (suffix.toLowerCase().equals(".jpg") || suffix.toLowerCase().equals(".jpeg") || suffix.toLowerCase().equals(".bmp") || suffix.toLowerCase().equals(".png") || suffix.toLowerCase().equals(".gif") ) { list.add(fi.getPath()); } } } return list; } }
而後定義一個圖像適配器類ImageAdaper,代碼以下:android-studio
package com.example.mcjproject; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import java.util.List; /** * Created by JorgeZhu on 2017/11/12. */ public class ImageAdapter extends BaseAdapter { private List<String> mImages; private Context mContext; public ImageAdapter(List<String> mImages,Context context){ this.mImages = mImages; mContext = context; } @Override public int getCount() { return mImages.size(); } @Override public Object getItem(int position) { return mImages.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); Bitmap bm=null; bm = BitmapFactory.decodeFile(mImages.get(position)); imageView.setImageBitmap(bm); //imageView.setImageResource(mImages.get(position)); return imageView; } }
最後是一個佈局文件pic_view.xml網絡
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.mcjproject.PicStackViewActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:textColor="#ff0000" android:text="當前位置" /> </LinearLayout> <StackView android:id="@+id/stackview" android:layout_width="match_parent" android:layout_height="match_parent" android:loopViews="false" /> </LinearLayout>