相册
java文件:
package com.drision.demo; import java.io.File; import java.util.ArrayList; import java.util.Calendar; import com.example1.demoview.R; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.OnHierarchyChangeListener; import android.widget.Button; import android.widget.HorizontalScrollView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.Toast; public class MainActivity extends Activity { private Button btn_camera; private MainActivity _this; private Uri fileUri; private HorizontalScrollView horizontalScrollView; private LinearLayout linearLayou; private ArrayList<ImageView> list = new ArrayList<ImageView>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontal); linearLayou = (LinearLayout) findViewById(R.id.gallery); _this = this; btn_camera = (Button) findViewById(R.id.button1); String path = Environment.getExternalStorageDirectory() + "/image"; File file = new File(path); if(!file.exists()){ file.mkdirs(); } String time = Calendar.getInstance().getTime().toString(); File mediaFile = new File(file.getAbsolutePath(),time + ".jpg"); fileUri = Uri.fromFile(mediaFile); btn_camera.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, 100); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == _this.RESULT_OK){ if(requestCode == 100){ ImageView image = new ImageView(_this); LinearLayout.LayoutParams llparams = new LinearLayout.LayoutParams(80, 80); image.setLayoutParams(llparams); if(data != null){ if(data.hasExtra("data")){ Bitmap thumbnail = data.getParcelableExtra("data"); image.setImageBitmap(thumbnail); } }else{ int width = image.getLayoutParams().width; int height = image.getLayoutParams().height; BitmapFactory.Options option = new Options(); option.inJustDecodeBounds = true; BitmapFactory.decodeFile(fileUri.getPath(), option); int imageW = option.outWidth; int imageH = option.outHeight; int scale = Math.max(imageW / width, imageH / height); option.inJustDecodeBounds = false; option.inSampleSize = scale; Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), option); image.setImageBitmap(bitmap); linearLayou.addView(image); if(linearLayou.getChildCount() > 0){ horizontalScrollView.setVisibility(View.VISIBLE); } } } } } }
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="wrap_content" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="Button" android:textSize="20sp" /> <HorizontalScrollView android:id="@+id/horizontal" android:layout_width="fill_parent" android:layout_height="90dp" android:layout_gravity="center_vertical" android:visibility="gone" android:scrollbars="none" > <LinearLayout android:id="@+id/gallery" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > </LinearLayout> </HorizontalScrollView> </LinearLayout>