实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.example.croppersample; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Typeface; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AdapterView; import android.widget.Button; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Spinner; import android.widget.TextView; import android.widget.ToggleButton; import com.edmodo.cropper.CropImageView; public class MainActivity extends Activity { // Static final constants private static final int DEFAULT_ASPECT_RATIO_VALUES = 10; private static final int ROTATE_NINETY_DEGREES = 90; private static final String ASPECT_RATIO_X = "ASPECT_RATIO_X"; private static final String ASPECT_RATIO_Y = "ASPECT_RATIO_Y"; private static final int ON_TOUCH = 1; // Instance variables private int mAspectRatioX = DEFAULT_ASPECT_RATIO_VALUES; private int mAspectRatioY = DEFAULT_ASPECT_RATIO_VALUES; Bitmap croppedImage; // Saves the state upon rotating the screen/restarting the activity @Override protected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(bundle); bundle.putInt(ASPECT_RATIO_X, mAspectRatioX); bundle.putInt(ASPECT_RATIO_Y, mAspectRatioY); } // Restores the state upon rotating the screen/restarting the activity @Override protected void onRestoreInstanceState(Bundle bundle) { super.onRestoreInstanceState(bundle); mAspectRatioX = bundle.getInt(ASPECT_RATIO_X); mAspectRatioY = bundle.getInt(ASPECT_RATIO_Y); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 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); } }); } /* * Sets the font on all TextViews in the ViewGroup. Searches recursively for * all inner ViewGroups as well. Just add a check for any other views you * want to set as well (EditText, etc.) */ public void setFont(ViewGroup group, Typeface font) { int count = group.getChildCount(); View v; for (int i = 0; i < count; i ) { v = group.getChildAt(i); if (v instanceof TextView || v instanceof EditText || v instanceof Button) { ((TextView) v).setTypeface(font); } else if (v instanceof ViewGroup) setFont((ViewGroup) v, font); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论