Search in sources :

Example 1 with CropImageView

use of com.edmodo.cropper.CropImageView in project cropper by edmodo.

the class MainActivity method onCreate.

// Activity Methods ////////////////////////////////////////////////////////////////////////////
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    // Initialize Views.
    final ToggleButton fixedAspectRatioToggleButton = (ToggleButton) findViewById(R.id.fixedAspectRatioToggle);
    final TextView aspectRatioXTextView = (TextView) findViewById(R.id.aspectRatioX);
    final SeekBar aspectRatioXSeekBar = (SeekBar) findViewById(R.id.aspectRatioXSeek);
    final TextView aspectRatioYTextView = (TextView) findViewById(R.id.aspectRatioY);
    final SeekBar aspectRatioYSeekBar = (SeekBar) findViewById(R.id.aspectRatioYSeek);
    final Spinner guidelinesSpinner = (Spinner) findViewById(R.id.showGuidelinesSpin);
    final CropImageView cropImageView = (CropImageView) findViewById(R.id.CropImageView);
    final ImageView croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
    final Button cropButton = (Button) findViewById(R.id.Button_crop);
    // Initializes fixedAspectRatio toggle button.
    fixedAspectRatioToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            cropImageView.setFixedAspectRatio(isChecked);
            cropImageView.setAspectRatio(aspectRatioXSeekBar.getProgress(), aspectRatioYSeekBar.getProgress());
            aspectRatioXSeekBar.setEnabled(isChecked);
            aspectRatioYSeekBar.setEnabled(isChecked);
        }
    });
    // Set seek bars to be disabled until toggle button is checked.
    aspectRatioXSeekBar.setEnabled(false);
    aspectRatioYSeekBar.setEnabled(false);
    aspectRatioXTextView.setText(String.valueOf(aspectRatioXSeekBar.getProgress()));
    aspectRatioYTextView.setText(String.valueOf(aspectRatioXSeekBar.getProgress()));
    // Initialize aspect ratio X SeekBar.
    aspectRatioXSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar aspectRatioXSeekBar, int progress, boolean fromUser) {
            if (progress < 1) {
                aspectRatioXSeekBar.setProgress(1);
            }
            cropImageView.setAspectRatio(aspectRatioXSeekBar.getProgress(), aspectRatioYSeekBar.getProgress());
            aspectRatioXTextView.setText(String.valueOf(aspectRatioXSeekBar.getProgress()));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        // Do nothing.
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        // Do nothing.
        }
    });
    // Initialize aspect ratio Y SeekBar.
    aspectRatioYSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar aspectRatioYSeekBar, int progress, boolean fromUser) {
            if (progress < 1) {
                aspectRatioYSeekBar.setProgress(1);
            }
            cropImageView.setAspectRatio(aspectRatioXSeekBar.getProgress(), aspectRatioYSeekBar.getProgress());
            aspectRatioYTextView.setText(String.valueOf(aspectRatioYSeekBar.getProgress()));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        // Do nothing.
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        // Do nothing.
        }
    });
    // Set up the Guidelines Spinner.
    guidelinesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            cropImageView.setGuidelines(i);
        }

        public void onNothingSelected(AdapterView<?> adapterView) {
        // Do nothing.
        }
    });
    guidelinesSpinner.setSelection(GUIDELINES_ON_TOUCH);
    // Initialize the Crop button.
    cropButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final Bitmap croppedImage = cropImageView.getCroppedImage();
            croppedImageView.setImageBitmap(croppedImage);
        }
    });
}
Also used : ToggleButton(android.widget.ToggleButton) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) SeekBar(android.widget.SeekBar) Spinner(android.widget.Spinner) OnSeekBarChangeListener(android.widget.SeekBar.OnSeekBarChangeListener) CropImageView(com.edmodo.cropper.CropImageView) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) Bitmap(android.graphics.Bitmap) CompoundButton(android.widget.CompoundButton) Button(android.widget.Button) ToggleButton(android.widget.ToggleButton) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) CropImageView(com.edmodo.cropper.CropImageView) ImageView(android.widget.ImageView) CropImageView(com.edmodo.cropper.CropImageView) CompoundButton(android.widget.CompoundButton)

Aggregations

Bitmap (android.graphics.Bitmap)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 Button (android.widget.Button)1 CompoundButton (android.widget.CompoundButton)1 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)1 ImageView (android.widget.ImageView)1 SeekBar (android.widget.SeekBar)1 OnSeekBarChangeListener (android.widget.SeekBar.OnSeekBarChangeListener)1 Spinner (android.widget.Spinner)1 TextView (android.widget.TextView)1 ToggleButton (android.widget.ToggleButton)1 CropImageView (com.edmodo.cropper.CropImageView)1