您好我想创建动态文件视图库图片,但我不知道如何创建动态文件视图.....请帮帮我......给我你的建议......
这是我的图库查看当前代码:
package ImageViewExample.ImageViewExample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
//import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class ImageViewExample extends Activity {
/** Called when the activity is first created. */
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
Gallery imagegrid;
private int count;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_image_grid();
}
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (Gallery) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(),
ViewImage.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
System.gc();
ImageView i = new ImageView(mContext.getApplicationContext());
if (convertView == null) {
imagecursor.moveToPosition(position);
int id = imagecursor.getInt(image_column_index);
i.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ id));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new Gallery.LayoutParams(110,110));
}
else {
i = (ImageView) convertView;
}
return i;
}
}
}
这是我的Xml编码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PhoneImageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:spacing="16dp"
/>
</LinearLayout>
log cat error:
04-25 23:06:14.329: INFO/Process(391): Sending signal. PID: 391 SIG: 9
04-25 23:06:14.389: INFO/ActivityManager(63): Process
ImageViewExample.ImageViewExample (pid 391) has died.
04-25 23:06:14.800: WARN/InputManagerService(63): Window already focused, ignoring
focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43fdc730
04-25 23:06:26.009: DEBUG/AndroidRuntime(403): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-25 23:06:26.019: DEBUG/AndroidRuntime(403): CheckJNI is ON
04-25 23:06:26.580: DEBUG/AndroidRuntime(403): --- registering native functions ---
04-25 23:06:28.259: DEBUG/AndroidRuntime(403): Shutting down VM
04-25 23:06:28.300: DEBUG/dalvikvm(403): Debugger has detached; object registry had 1
entries
04-25 23:06:28.329: INFO/AndroidRuntime(403): NOTE: attach of thread 'Binder Thread
#3' failed
04-25 23:06:29.389: DEBUG/AndroidRuntime(411): >>>>>>>>>>>>>> AndroidRuntime START
<<<<<<<<<<<<<<
04-25 23:06:29.389: DEBUG/AndroidRuntime(411): CheckJNI is ON
04-25 23:06:29.839: DEBUG/AndroidRuntime(411): --- registering native functions ---
04-25 23:06:31.359: INFO/ActivityManager(63): Starting activity: Intent {
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000
cmp=ImageViewExample.ImageViewExample/.ImageViewExample }
04-25 23:06:31.610: DEBUG/AndroidRuntime(411): Shutting down VM
04-25 23:06:31.730: DEBUG/dalvikvm(411): Debugger has detached; object registry had 1
entries
04-25 23:06:31.786: INFO/AndroidRuntime(411): NOTE: attach of thread 'Binder Thread
#3' failed
04-25 23:06:32.019: INFO/ActivityManager(63): Start proc
ImageViewExample.ImageViewExample for activity
ImageViewExample.ImageViewExample/.ImageViewExample: pid=418 uid=10050 gids={}
04-25 23:06:33.169: DEBUG/AndroidRuntime(418): Shutting down VM
04-25 23:06:33.169: WARN/dalvikvm(418): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): FATAL EXCEPTION: main
04-25 23:06:33.229: ERROR/AndroidRuntime(418): java.lang.RuntimeException: Unable to
start activity
ComponentInfo{ImageViewExample.ImageViewExample/ImageViewExample.ImageViewExample
.ImageViewExample}: java.lang.NullPointerException
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.os.Looper.loop(Looper.java:123)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.main(ActivityThread.java:4627)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
java.lang.reflect.Method.invoke(Method.java:521)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
dalvik.system.NativeStart.main(Native Method)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): Caused by:
java.lang.NullPointerException
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
ImageViewExample.ImageViewExample.MyImageView.<init>(MyImageView.java:32)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
ImageViewExample.ImageViewExample.ImageViewExample.onCreate(ImageViewExample.java:28)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-25 23:06:33.229: ERROR/AndroidRuntime(418): ... 11 more
04-25 23:06:33.279: WARN/ActivityManager(63): Force finishing activity
ImageViewExample.ImageViewExample/.ImageViewExample
04-25 23:06:33.839: WARN/ActivityManager(63): Activity pause timeout for
HistoryRecord{4403fb30 ImageViewExample.ImageViewExample/.ImageViewExample}
04-25 23:06:45.281: WARN/ActivityManager(63): Activity destroy timeout for
HistoryRecord{4403fb30 ImageViewExample.ImageViewExample/.ImageViewExample}
04-25 23:10:06.659: DEBUG/SntpClient(63): request time failed:
java.net.SocketException:
Address family not supported by protocol
答案 0 :(得分:0)
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
public class MyImageView extends View {
private static final int INVALID_POINTER_ID = -1;
private Drawable mImage;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
public MyImageView(Context context) {
this(context, null, 0);
mImage = Drawable.createFromPath("/sdcard/filename");
mImage.setBounds(0, 0, width, height);
}
public MyImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
Log.d("DEBUG", "X: "+mPosX+" Y: "+mPosY);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
mImage.draw(canvas);
canvas.restore();
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
invalidate();
return true;
}
}
}
在你的活动中调用它,setContentView(new MyImageView(this));