它实际上是一个图像库代码,由gridlayout组成,显示存储在SD卡文件夹中的图像,点击网格图像时,它将导航到另一个布局(full_screen)包含如下所示的查看寻呼机,
FullScreenActivityjava
package com.example.imagegallery;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.example.imagegallery.R;
import com.example.imagegallery.image.adapter.FullScreenImageAdapter;
import com.example.imagegallery.image.help.*;
public class FullScreenViewActivity extends Activity{
private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.full_screen);
viewPager=(ViewPager)findViewById(R.id.pager);
utils=new Utils(getApplicationContext());
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter=new FullScreenImageAdapter(FullScreenViewActivity.this, utils.getFilePaths());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(position);
}
}
>and here the Adapter for the above class
FullScreenImageAdapter.java
---------------------------
package com.example.imagegallery.image.adapter;
import com.example.imagegallery.image.help.TouchImageView;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.example.imagegallery.R;
@SuppressWarnings("unused")
public class FullScreenImageAdapter extends PagerAdapter{
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
public FullScreenImageAdapter(Activity activity,ArrayList<String> imagePaths) {
this._activity=activity;
this._imagePaths=imagePaths;
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view==((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
TouchImageView imgDisplay;
Button btnClose;
inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout =inflater.inflate(R.layout.layout_fullscreen, container,false);
imgDisplay=(TouchImageView)viewLayout.findViewById(R.id.imgDisplay);
btnClose=(Button)viewLayout.findViewById(R.id.btnClose);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig= Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
imgDisplay.setImageBitmap(bitmap);
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
_activity.finish();
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((RelativeLayout) object);
}
}
> here the grid adapter
GridViewAdapt.java
------------------
package com.example.imagegallery.image.adapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import com.example.imagegallery.FullScreenViewActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class GridViewAdapt extends BaseAdapter {
private Activity _activity;
private ArrayList<String> _filePaths = new ArrayList<String>();
private int imageWidth;
public GridViewAdapt(Activity activity, ArrayList<String> filepaths,int imageWidth){
this._activity=activity;
this._filePaths=filepaths;
this.imageWidth=imageWidth;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this._filePaths.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this._filePaths.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(_activity);
} else {
imageView = (ImageView) convertView;
}
// get screen dimensions
Bitmap image = decodeFile(_filePaths.get(position), imageWidth,
imageWidth);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
imageWidth));
imageView.setImageBitmap(image);
// image view click listener
imageView.setOnClickListener(new OnImageClickListener(position));
// TODO Auto-generated method stub
return imageView;
}
class OnImageClickListener implements OnClickListener{
int _position;
public OnImageClickListener( int position)
{
this._position=position;
}
@Override
public void onClick(View v) {
Intent i = new Intent(_activity,FullScreenViewActivity.class);
i.putExtra("position", _position);
_activity.startActivity(i);
// TODO Auto-generated method stub
}
}
private Bitmap decodeFile(String filepath, int WIDTH, int HIGHT) {
try{
File f = new File(filepath);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_WIDTH = WIDTH;
final int REQUIRED_HIGHT = HIGHT;
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
&& o.outHeight / scale / 2 >= REQUIRED_HIGHT)
scale *= 2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
// TODO Auto-generated method stub
return null;
}
}
> here the grid activity
GridViewActivity.java
---------------------
package com.example.imagegallery;
import com.example.imagegallery.image.help.*;
import java.util.ArrayList;
import com.example.imagegallery.image.adapter.GridViewAdapt;
import com.example.imagegallery.image.help.Utils;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Menu;
import android.widget.GridView;
public class GridActivity extends Activity {
private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewAdapt adapter;
private GridView gridView;
private int columnWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid);
gridView = (GridView) findViewById(R.id.grid_view);
utils = new Utils(this);
InitializGridLayout();
imagePaths = utils.getFilePaths();
adapter = new GridViewAdapt(GridActivity.this, imagePaths, columnWidth);
gridView.setAdapter(adapter);
}
private void InitializGridLayout() {
// TODO Auto-generated method stub
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
AppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
}
全屏幕的XML
Full_Screen.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"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Layout_Fullscreen.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.imagegallery.image.help.TouchImageView
android:id="@+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="@drawable/button_background"
android:textColor="#ffffff"
android:text="Close" />
</RelativeLayout>``
答案 0 :(得分:0)
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_grid);