Java 拍摄相机图像并添加到android中的滑块
我是android新手,我正在尝试添加从相机拍摄的图像,并将它们添加到Java 拍摄相机图像并添加到android中的滑块,java,android,xml,android-layout,Java,Android,Xml,Android Layout,我是android新手,我正在尝试添加从相机拍摄的图像,并将它们添加到查看页面中。它应该看起来像一个图像旋转木马。这是我到目前为止尝试过的,但问题是相同的图像被添加了5次。 我想知道我可以在我的CustomAdapter的getCount()方法中更改什么,以便将不同的图像发送到转盘 我的主要活动 public class MainActivity extends AppCompatActivity { public final static int CAPTURE_IMAGE_ACTIVIT
查看页面中。它应该看起来像一个图像旋转木马。这是我到目前为止尝试过的,但问题是相同的图像被添加了5次。
我想知道我可以在我的CustomAdapter
的getCount()
方法中更改什么,以便将不同的图像发送到转盘
我的主要活动
public class MainActivity extends AppCompatActivity {
public final static int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1034;
public String photoFileName = "photo.jpg";
public final String APP_TAG = "MyCustomApp";
ViewPager viewPager;
CustomSwipeAdapter adapter;
ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
viewPager = (ViewPager) findViewById(R.id.view_pager);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
});
}
//ON ACTIVITY RESULT..
protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri takenPhotoUri = getPhotoFileUri(photoFileName);
Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
adapter = new CustomSwipeAdapter(this, iv, takenImage);
viewPager.setAdapter(adapter);
} else {
Toast.makeText(this, "Picture Wasn't taken!", Toast.LENGTH_SHORT).show();
}
}
}
public Uri getPhotoFileUri(String fileName) {
if (isExternalStorageAvailable()) {
File mediaStoreDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);
if (!mediaStoreDir.exists() && !mediaStoreDir.mkdirs()) {
Log.d(APP_TAG, "Failed to create Directory");
}
return Uri.fromFile(new File(mediaStoreDir.getPath() + File.separator + fileName));
}
return null;
}
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED);
}
自定义适配器
public class CustomSwipeAdapter extends PagerAdapter {
private Context ctx;
private LayoutInflater layoutInflater;
ImageView iv;
Bitmap imageTaken;
public CustomSwipeAdapter(Context ctx, ImageView iv, Bitmap imageTaken) {
this.ctx = ctx;
this.iv = iv;
this.imageTaken = imageTaken;
}
@Override
public int getCount() {
return 5; //Need to get Image Count here...
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
TextView tv = (TextView) item_view.findViewById(R.id.image_count);
iv = (ImageView) item_view.findViewById(R.id.imageView1);
iv.setImageBitmap(imageTaken);
tv.setText("Image" + position);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
活动\u main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id ="@+id/view_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Take a Photo"></Button>
</RelativeLayout>
<?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">
<TextView
android:id="@+id/image_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center"
android:text="Hello World"
android:textSize="25dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp" />
</LinearLayout>
滑动布局.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id ="@+id/view_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Take a Photo"></Button>
</RelativeLayout>
<?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">
<TextView
android:id="@+id/image_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center"
android:text="Hello World"
android:textSize="25dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp" />
</LinearLayout>
您看到一个图像5次,因为您只向适配器构造函数提供一个位图并显示该图像。
请将从相机拍摄的新图像添加到ArrayList位图列表
,并将该位图传递给适配器
在getView方法中,从arralist中获取位图并显示在旋转木马中
希望这对您有所帮助。谢谢!这很有效!从未想过将图像添加到ArrayList。但我现在已经修好了。:)谢谢你接受这个答案,我不知道谁对你的问题投了赞成票。