Search in sources :

Example 1 with CropImageView

use of com.marshalchen.common.uimodule.cropperimage.CropImageView in project UltimateAndroid by cymcsg.

the class CropperSample method onCreate.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.cropper_activity);
    // Sets fonts for all
    Typeface mFont = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
    ViewGroup root = (ViewGroup) findViewById(R.id.mylayout);
    setFont(root, mFont);
    // Initialize components of the app
    final CropImageView cropImageView = (CropImageView) findViewById(R.id.CropImageView);
    final SeekBar aspectRatioXSeek = (SeekBar) findViewById(R.id.aspectRatioXSeek);
    final SeekBar aspectRatioYSeek = (SeekBar) findViewById(R.id.aspectRatioYSeek);
    final ToggleButton fixedAspectRatioToggle = (ToggleButton) findViewById(R.id.fixedAspectRatioToggle);
    Spinner showGuidelinesSpin = (Spinner) findViewById(R.id.showGuidelinesSpin);
    // Sets sliders to be disabled until fixedAspectRatio is set
    aspectRatioXSeek.setEnabled(false);
    aspectRatioYSeek.setEnabled(false);
    // Set initial spinner value
    showGuidelinesSpin.setSelection(ON_TOUCH);
    //Sets the rotate button
    final Button rotateButton = (Button) findViewById(R.id.Button_rotate);
    rotateButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            cropImageView.rotateImage(ROTATE_NINETY_DEGREES);
        }
    });
    // Sets fixedAspectRatio
    fixedAspectRatioToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            cropImageView.setFixedAspectRatio(isChecked);
            if (isChecked) {
                aspectRatioXSeek.setEnabled(true);
                aspectRatioYSeek.setEnabled(true);
            } else {
                aspectRatioXSeek.setEnabled(false);
                aspectRatioYSeek.setEnabled(false);
            }
        }
    });
    // Sets initial aspect ratio to 10/10, for demonstration purposes
    cropImageView.setAspectRatio(DEFAULT_ASPECT_RATIO_VALUES, DEFAULT_ASPECT_RATIO_VALUES);
    // Sets aspectRatioX
    final TextView aspectRatioX = (TextView) findViewById(R.id.aspectRatioX);
    aspectRatioXSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar aspectRatioXSeek, int progress, boolean fromUser) {
            try {
                mAspectRatioX = progress;
                cropImageView.setAspectRatio(progress, mAspectRatioY);
                aspectRatioX.setText(" " + progress);
            } catch (IllegalArgumentException e) {
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
    // Sets aspectRatioY
    final TextView aspectRatioY = (TextView) findViewById(R.id.aspectRatioY);
    aspectRatioYSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar aspectRatioYSeek, int progress, boolean fromUser) {
            try {
                mAspectRatioY = progress;
                cropImageView.setAspectRatio(mAspectRatioX, progress);
                aspectRatioY.setText(" " + progress);
            } catch (IllegalArgumentException e) {
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
    // Sets up the Spinner
    showGuidelinesSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

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

        public void onNothingSelected(AdapterView<?> adapterView) {
            return;
        }
    });
    final Button cropButton = (Button) findViewById(R.id.Button_crop);
    cropButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            croppedImage = cropImageView.getCroppedImage();
            ImageView croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
            croppedImageView.setImageBitmap(croppedImage);
        }
    });
}
Also used : ToggleButton(android.widget.ToggleButton) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) SeekBar(android.widget.SeekBar) Typeface(android.graphics.Typeface) ViewGroup(android.view.ViewGroup) Spinner(android.widget.Spinner) OnSeekBarChangeListener(android.widget.SeekBar.OnSeekBarChangeListener) ImageView(android.widget.ImageView) CropImageView(com.marshalchen.common.uimodule.cropperimage.CropImageView) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) CompoundButton(android.widget.CompoundButton) Button(android.widget.Button) ToggleButton(android.widget.ToggleButton) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) ImageView(android.widget.ImageView) CropImageView(com.marshalchen.common.uimodule.cropperimage.CropImageView) CropImageView(com.marshalchen.common.uimodule.cropperimage.CropImageView) CompoundButton(android.widget.CompoundButton)

Aggregations

Typeface (android.graphics.Typeface)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)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.marshalchen.common.uimodule.cropperimage.CropImageView)1