触摸图像视图不起作用
Touch to image view not working
我制作了一个简单的应用程序,我在其中使用了 3 个选项卡。每个选项卡都有单独的 classes
和 layouts
。第一个选项卡显示地图。第二个选项卡将显示用户可以从中拍摄照片的相机,第三个选项卡关闭 Pictures,其中在 GridView
中查看捕获的图像。所有选项卡都运行良好。现在我想要的是在用户点击任何图像时显示完整的 ImageView
。
对于所有这些,我都使用这个 tutorial。所以我跟进了其中的每一步,一切都很顺利。但是对于完整的 ImageView
代码不起作用。
下面是我的照片Fragment
Pictures.java
public class Pictures extends Fragment implements MainActivity.Updateable {
private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pictures, container, false);
gridView = (GridView) rootView.findViewById(R.id.grid_view);
utils = new Utils(getContext());
// Initilizing Grid View
InitilizeGridLayout();
// loading all image paths from SD card
imagePaths = utils.getFilePaths();
// Gridview adapter
adapter = new GridViewImageAdapter(getActivity(), imagePaths, columnWidth);
/*adapter = new GridViewImageAdapter(Pictures.this, imagePaths,
columnWidth);*/
// setting grid view adapter
gridView.setAdapter(adapter);
//((BaseAdapter) adapter).notifyDataSetChanged();
//adapter.notifyDataSetChanged();
return rootView;
}
private void InitilizeGridLayout() {
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);
}
@Override
public void update() {
if(adapter !=null)
{
adapter.notifyDataSetChanged();
}
else
{
Log.d(getTag(),"null");
}
}}
现在为了全屏视图,我制作了一个新的 Activity
两个单独的 Layouts
一个用于屏幕,一个用于图像,如下所示
fullscreen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /></LinearLayout>
fullscreenImage.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
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>
现在我创建了一个 Adapter
class
FullScreenImageAdapter.java
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
false);
imgDisplay = (ImageView) 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);
// close button click event
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_activity.finish();
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}}
现在全屏Activity
FullScreenViewActivity.java
public class FullScreenViewActivity extends Activity {
private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
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);
// displaying selected image first
viewPager.setCurrentItem(position);
}}
现在,当我点击任何图片时,没有任何反应,logcat
上没有任何错误,没有任何警告,也没有任何应用程序崩溃。
我已经调试了应用程序,但还没有到那个地步。
卡住了,不知道怎么办
非常感谢任何帮助。
这可能不是您要找的答案!但是,如果您的代码仍然不起作用,这将帮助您实现 solution.If 您知道整个过程,您可以即插即用。
这个答案来自您关注的 tutorial 同一个人,他与您的代码有某种相同的模式。注意图像数组是 hard-coded.
看看,在AndroidGridLayoutActivity
特别注意!
启动器Activity:HomeActivity
您在清单中需要的其他activity:FullImageActivity
grid_layout.xml >
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center">
</GridView>
full_image.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" >
<ImageView android:id="@+id/full_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout
主页Activity >
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
AndroidGridLayoutActivity>(查看这里的评论)
public class AndroidGridLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//----------------------Seems In your code you are missing this Part----------------------
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
ImageAdapter >
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array - I have hard coded this
public Integer[] mThumbIds = {
R.drawable.casper, R.drawable.casper,
R.drawable.monkey, R.drawable.monkey,
R.drawable.cub, R.drawable.cub,
R.drawable.mouse, R.drawable.mouse,
R.drawable.casper, R.drawable.casper,
R.drawable.monkey, R.drawable.monkey,
R.drawable.cub, R.drawable.cub,
R.drawable.mouse
};
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150)); /// IF YOU WANT TO CHANGE IMAGE SIZE CHANGE HERE
return imageView;
}
}
作为一个单独的项目,如果您无法即插即用地解决问题,这将 work.In 案例 this.Just 发帖作为帮助!
输出 >
您可以尝试在您的网格视图上使用 OnItemClickListener,如下所示
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Get your item here
adapterView.getItemAtPosition(i);
//Do your code here
}
});
请确保您从 getView 中删除了点击监听器,否则会产生冲突。
请确保您的意图是单图查看 class
将位置 id 发送到 imageview class 我使用下面的代码进行全屏图像查看
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext, Singleimageview.class);
i.putExtra("pos", (Integer) itemView.getTag() + "");
mContext.startActivity(i);
overridePendingTransition(R.anim.pull_in_left, R.anim.pull_out_right);
}
});
我制作了一个简单的应用程序,我在其中使用了 3 个选项卡。每个选项卡都有单独的 classes
和 layouts
。第一个选项卡显示地图。第二个选项卡将显示用户可以从中拍摄照片的相机,第三个选项卡关闭 Pictures,其中在 GridView
中查看捕获的图像。所有选项卡都运行良好。现在我想要的是在用户点击任何图像时显示完整的 ImageView
。
对于所有这些,我都使用这个 tutorial。所以我跟进了其中的每一步,一切都很顺利。但是对于完整的 ImageView
代码不起作用。
下面是我的照片Fragment
Pictures.java
public class Pictures extends Fragment implements MainActivity.Updateable {
private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pictures, container, false);
gridView = (GridView) rootView.findViewById(R.id.grid_view);
utils = new Utils(getContext());
// Initilizing Grid View
InitilizeGridLayout();
// loading all image paths from SD card
imagePaths = utils.getFilePaths();
// Gridview adapter
adapter = new GridViewImageAdapter(getActivity(), imagePaths, columnWidth);
/*adapter = new GridViewImageAdapter(Pictures.this, imagePaths,
columnWidth);*/
// setting grid view adapter
gridView.setAdapter(adapter);
//((BaseAdapter) adapter).notifyDataSetChanged();
//adapter.notifyDataSetChanged();
return rootView;
}
private void InitilizeGridLayout() {
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);
}
@Override
public void update() {
if(adapter !=null)
{
adapter.notifyDataSetChanged();
}
else
{
Log.d(getTag(),"null");
}
}}
现在为了全屏视图,我制作了一个新的 Activity
两个单独的 Layouts
一个用于屏幕,一个用于图像,如下所示
fullscreen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /></LinearLayout>
fullscreenImage.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
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>
现在我创建了一个 Adapter
class
FullScreenImageAdapter.java
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
false);
imgDisplay = (ImageView) 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);
// close button click event
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_activity.finish();
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}}
现在全屏Activity
FullScreenViewActivity.java
public class FullScreenViewActivity extends Activity {
private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
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);
// displaying selected image first
viewPager.setCurrentItem(position);
}}
现在,当我点击任何图片时,没有任何反应,logcat
上没有任何错误,没有任何警告,也没有任何应用程序崩溃。
我已经调试了应用程序,但还没有到那个地步。
卡住了,不知道怎么办
非常感谢任何帮助。
这可能不是您要找的答案!但是,如果您的代码仍然不起作用,这将帮助您实现 solution.If 您知道整个过程,您可以即插即用。
这个答案来自您关注的 tutorial 同一个人,他与您的代码有某种相同的模式。注意图像数组是 hard-coded.
看看,在AndroidGridLayoutActivity
特别注意!
启动器Activity:HomeActivity
您在清单中需要的其他activity:FullImageActivity
grid_layout.xml >
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center">
</GridView>
full_image.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" >
<ImageView android:id="@+id/full_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout
主页Activity >
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
AndroidGridLayoutActivity>(查看这里的评论)
public class AndroidGridLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//----------------------Seems In your code you are missing this Part----------------------
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
ImageAdapter >
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array - I have hard coded this
public Integer[] mThumbIds = {
R.drawable.casper, R.drawable.casper,
R.drawable.monkey, R.drawable.monkey,
R.drawable.cub, R.drawable.cub,
R.drawable.mouse, R.drawable.mouse,
R.drawable.casper, R.drawable.casper,
R.drawable.monkey, R.drawable.monkey,
R.drawable.cub, R.drawable.cub,
R.drawable.mouse
};
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150)); /// IF YOU WANT TO CHANGE IMAGE SIZE CHANGE HERE
return imageView;
}
}
作为一个单独的项目,如果您无法即插即用地解决问题,这将 work.In 案例 this.Just 发帖作为帮助!
输出 >
您可以尝试在您的网格视图上使用 OnItemClickListener,如下所示
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Get your item here
adapterView.getItemAtPosition(i);
//Do your code here
}
});
请确保您从 getView 中删除了点击监听器,否则会产生冲突。
请确保您的意图是单图查看 class
将位置 id 发送到 imageview class 我使用下面的代码进行全屏图像查看
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext, Singleimageview.class);
i.putExtra("pos", (Integer) itemView.getTag() + "");
mContext.startActivity(i);
overridePendingTransition(R.anim.pull_in_left, R.anim.pull_out_right);
}
});