Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- 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.ImageButton;
- import android.widget.TextView;
- import org.lucasr.twowayview.TwoWayView;
- import java.util.ArrayList;
- public class MainActivity extends Activity {
- public static ArrayList<Uri> uris = new ArrayList<>();
- private static ContactPagerAdapter adapter;
- public static Integer ids;
- private static int focusedPage;
- private static final int SMOOTH_SCROLL = 0;
- private static final int SET_SELECT_SCROLL = 1;
- public static Boolean resultState;
- private static TwoWayView lvTest;
- private static TouchImageView photoImage;
- private static ViewPager pager;
- private TextView textView;
- private ImageButton imageButton;
- private ImageButton ibtnLeft;
- private ImageButton ibtnRight;
- private Intent cameraIntent;
- private CustomAdapter customAdapter;
- private LayoutInflater inflater;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- resultState = false;
- inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
- focusedPage = 0;
- textView = (TextView) findViewById(R.id.textView);
- lvTest = (TwoWayView) findViewById(R.id.lvItems);
- pager = (ViewPager) findViewById(R.id.pager);
- imageButton = (ImageButton) findViewById(R.id.imageButton);
- ibtnLeft = (ImageButton) findViewById(R.id.imageButtonLeft);
- ibtnRight = (ImageButton) findViewById(R.id.imageButtonRight);
- adapter = new ContactPagerAdapter();
- ibtnRight.setVisibility(View.INVISIBLE);
- ibtnLeft.setVisibility(View.INVISIBLE);
- imageButton.setVisibility(View.INVISIBLE);
- customAdapter = new CustomAdapter(this, uris, inflater);
- lvTest.setAdapter(customAdapter);
- pager.setAdapter(adapter);
- cameraIntent = new Intent(this, CameraActivity.class);
- pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
- @Override
- public void onPageSelected(int position) {
- focusedPage = position;
- if (position == uris.size())
- pager.setCurrentItem(0);
- setScrollTop(focusedPage, SMOOTH_SCROLL);
- if (position == uris.size() - 1) {
- ibtnRight.setVisibility(View.INVISIBLE);
- } else {
- ibtnRight.setVisibility(View.VISIBLE);
- }
- if (position == 0) {
- ibtnLeft.setVisibility(View.INVISIBLE);
- } else {
- ibtnLeft.setVisibility(View.VISIBLE);
- }
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- });
- }
- public class ContactPagerAdapter extends PagerAdapter {
- public ContactPagerAdapter() {
- super();
- }
- @Override
- public int getItemPosition(Object object) {
- return POSITION_NONE;
- }
- @Override
- public int getCount() {
- return uris.size();
- }
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- if (uris.size() != 0) {
- photoImage = new TouchImageView(getApplicationContext());
- // photoImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
- photoImage.setImageURI(uris.get(position));
- // Picasso.with(context).load(uris.get(position)).into(photoImage);
- /*final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
- photoImage.setPadding(padding, padding, padding, padding);*/
- container.addView(photoImage, 0);
- photoImage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- }
- });
- }
- return photoImage;
- }
- @Override
- public void destroyItem(ViewGroup container, int position, Object view) {
- container.removeView((View) view);
- }
- @Override
- public boolean isViewFromObject(View v, Object o) {
- return v == o;
- }
- }
- public static void changePhoto(int id) {
- pager.setCurrentItem(id);
- ids = id;
- }
- @Override
- public void onBackPressed() {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
- alertDialog.setTitle("Выйти?");
- alertDialog.setPositiveButton("Да", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- System.exit(0);
- }
- });
- alertDialog.setNegativeButton("Нет", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- alertDialog.show();
- }
- /* public static void resizeImageView(Uri uri) {
- File picture = new File(String.valueOf(uri));
- if (picture.exists()) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 0;
- // Bitmap myBitmap = BitmapFactory.decodeFile(picture.getAbsolutePath(), options);
- }
- }*/
- public void onLeft(View v) {
- pager.setCurrentItem(0);
- }
- public void onRight(View v) {
- pager.setCurrentItem(uris.size() - 1);
- }
- public void setScrollTop(int pos, int scroll) {
- switch (scroll) {
- case 0:
- lvTest.smoothScrollToPosition(pos);
- break;
- case 1:
- lvTest.setSelection(pos);
- break;
- }
- }
- @Override
- public void onResume() {
- super.onResume();
- if (resultState) {
- refreshView();
- }
- }
- public void startCamera(View v) {
- setVisibility();
- startActivity(cameraIntent);
- resultState = true;
- }
- public void onClickPhotoNew(View v) {
- ids = null;
- setVisibility();
- startActivity(cameraIntent);
- resultState = true;
- }
- private void setVisibility(){
- textView.setVisibility(View.INVISIBLE);
- imageButton.setVisibility(View.VISIBLE);
- }
- public void refreshView() {
- customAdapter.notifyDataSetChanged();
- adapter.notifyDataSetChanged();
- if (ids == null) {
- pager.setCurrentItem(uris.size() - 1);
- } else {
- pager.setCurrentItem(ids);
- }
- setScrollTop(uris.size() - 1, SMOOTH_SCROLL);
- if (ids != null) {
- setScrollTop(ids, SET_SELECT_SCROLL);
- } else {
- lvTest.setSelection(uris.size() - 1);
- }
- ids = null;
- }
- public void deletePhoto(int id) {
- uris.remove(id);
- // uris.removeAll(Collections.singleton(null));
- /* for (int i = id; i < uris.size()-1-id; i++) {
- if (i<i+1){
- uris.set(i,uris.get(i+1));
- }
- }*/
- refreshView();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment