我想在点击图片时在全屏模式下显示图像

时间:2014-05-22 09:02:31

标签: android fullscreen aspect-ratio

我创建了一个应用。当我点击特定图像时,我希望它在下一页的全屏显示。

当我点击任何图像时,它们看起来非常模糊和像素化。

请有人帮助我。

这是我的代码及其link

package com.shranick.clubinthechi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class EventDetailCoverflow {

Context mContext;
public CoverFlow coverFlow;
ImageAdapter coverImageAdapter;
DisplayImageOptions options;
int selectedItem = -1;
ImageView selectedImage;
OutputStream fOut = null;
File f;
ArrayList imageToSave = new ArrayList();
ArrayList loaded = new ArrayList();


/** Called when the activity is first created. */

public EventDetailCoverflow(Context context, int scrollToIndex, TCImageLoader loader)
{
mContext = context;

options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.flyer_placeholder)
.showImageForEmptyUri(R.drawable.flyer_placeholder)
.imageScaleType(ImageScaleType.EXACTLY)
.cacheInMemory(true)
.build();


coverFlow = new CoverFlow(context);

coverImageAdapter = new ImageAdapter(mContext);

coverFlow.setAdapter(coverImageAdapter);

coverFlow.setSpacing(-25);
// coverFlow.setSelection(scrollToIndex, true);
coverFlow.setAnimationDuration(1000);
coverFlow.setGravity(Gravity.CENTER_VERTICAL);
coverFlow.setLayoutParams(new CoverFlow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

// GET SCREEN SIZE
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);

coverImageAdapter.coverflowWidth = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));
coverImageAdapter.coverflowHeight = (int) Math.ceil(dm.heightPixels * (dm.densityDpi / 160.0)) / 2;

coverFlow.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView parent, View view, int position, long id){

selectedItem = position;
selectedImage = (ImageView) view;

// custom dialog
final Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.eventdetail_dialog);

ImageButton favBtn = (ImageButton) dialog.findViewById(R.id.favo);
favBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

dialog.dismiss();

if(DataContainer.sharedManager().favoriteEvents == null)
DataContainer.sharedManager().getSavedFavoriteEvents(mContext);
DataContainer.sharedManager().saveToFavoriteEvents(DataContainer.sharedManager().eventItemsList.get(selectedItem), mContext);
}
});

ImageButton saveBtn = (ImageButton) dialog.findViewById(R.id.save);
saveBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

dialog.dismiss();

if(loaded.get(selectedItem) == true)
{
String strDirectory = Environment.getExternalStorageDirectory().toString();

f = null;
//   f = new File(strDirectory, generateRandomName());

f = new File(strDirectory, "");

try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
Log.e("MSG", "exception with fout start", e1);
}

/**Compress image**/
Bitmap bmp = ((BitmapDrawable)selectedImage.getDrawable()).getBitmap();
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);

try {
fOut.flush();
fOut.close();
Log.i("MSG","reached till fout");
} catch (IOException e) {
e.printStackTrace();
Log.e("MSG", "exception with fout", e);

}

MainActivity.instance.displayToastWithText("Image saved successfully");
}

else
{
MainActivity.instance.displayToastWithText("Wait, Image has not been loaded.");

if(!imageToSave.contains(selectedItem))
{
imageToSave.add(selectedItem);
} 
}

}
});
dialog.show();
}

});

}

String generateRandomName()
{
String text = "";
String possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( int i=0; i < 5; i++ )
text += possible.charAt((int)Math.floor(Math.random() * possible.length()));

return text;
}

public void reloadCoverflow()
{
coverImageAdapter.notifyDataSetChanged();
}

public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
public int coverflowHeight, coverflowWidth;
private Context mContext;

public ImageAdapter(Context c) {
mContext = c;

for(int i = 0 ; i < DataContainer.sharedManager().eventItemsList.size(); i++)
{
loaded.add(false);
}
}

public int getCount() {
return DataContainer.sharedManager().eventItemsList.size();
}

public Object getItem(int position) {
return DataContainer.sharedManager().eventItemsList.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

ImageView img = new ImageView(mContext);
img.setLayoutParams(new Gallery.LayoutParams(coverflowWidth, coverflowHeight));
img.setScaleType(ScaleType.CENTER_INSIDE);
img.setTag(position);
EventItem data = (EventItem) getItem(position);

ImageLoader.getInstance().displayImage(data.imageURL, img, options, new SimpleImageLoadingListener()

{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
ImageView image = (ImageView) view;
Log.i("MSG", "loading complete in view");

image.setImageBitmap(loadedImage);

int pos = Integer.parseInt(image.getTag().toString());
loaded.set(pos, true);

if(imageToSave.contains(image.getTag()))//means save it
{
String strDirectory = Environment.getExternalStorageDirectory().toString();

f = null;
f = new File(strDirectory, generateRandomName());

try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
Log.e("MSG", "exception with fout start", e1);
}

/**Compress image**/
Bitmap bmp = ((BitmapDrawable)image.getDrawable()).getBitmap();
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);

try {
fOut.flush();
fOut.close();
Log.i("MSG","reached till fout");
} catch (IOException e) {
e.printStackTrace();
Log.e("MSG", "exception with fout", e);

}
imageToSave.remove(image.getTag());
MainActivity.instance.displayToastWithText("Image saved successfully");

}
else
{
EventItem _data = (EventItem) getItem(Integer.parseInt(image.getTag().toString()));

ImageLoader.getInstance().displayImage(_data.imageURL, image, options, null);
}   

}
});

return img;

}
/** Returns the size (0.0f to 1.0f) of the views 
* depending on the 'offset' to the center. */ 
public float getScale(boolean focused, int offset) { 
/* Formula: 1 / (2 ^ offset) */ 
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
} 

public Bitmap getBitmapFromView(View view, int index) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.LTGRAY);

//draw text
FeaturedFlyerData data = (FeaturedFlyerData) getItem(index);

// paint to write text with
Paint paint = new Paint(); 
paint.setStyle(Style.FILL); 
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setTextSize(20);
// min. rect of text
Rect textBounds = new Rect();
paint.getTextBounds(data.name, 0, data.name.length(), textBounds);
canvas.drawText(data.name, 15, 10, paint);

paint.setTypeface(Typeface.SANS_SERIF);
paint.setTextSize(14);
canvas.drawText(data.location, 15, 40, paint);
canvas.drawText(data.area, 15, 70, paint);
canvas.drawText(data.info, 15, 100, paint);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}

}
}

请有人帮助我

3 个答案:

答案 0 :(得分:0)

你必须保持两个小图像一个小而另一个大屏幕尺寸

答案 1 :(得分:0)

没有编码部分,问题很容易看出。

扩展图像(由此我谈论将图像扩展到超出它的默认宽度和高度)将始终看起来模糊和像素化,因为它不是要扩展,只是矢量化图像可以做到这一点。

您必须找到一个技巧,使用非常大的图像并将其调整到所需的分辨率或使用矢量化图像。

答案 2 :(得分:0)

  1. 根据您需要的尺寸在ImageView中显示图像
  2. 在oncreate方法中计算屏幕大小
  3. onclick of image将图像大小设置为屏幕大小