May 20, 2014
May 19, 2014
Save something in SharedPreferences using SharedPreferences.Editor
If you want to save something (String, setting, value...) in SharedPreferences, such that it can be retrieved later after Activity exited, we can use SharedPreferences.Editor.
Example:
Example:
package com.example.androidsharedpreferences; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; public class MainActivity extends Activity { String[] dayOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; EditText editText; Button buttonSave; Spinner spinner; SharedPreferences prefs; final String KEY_SavedText = "Saved Text"; final String KEY_SavedSel = "Saved Selection"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.edittext); buttonSave = (Button)findViewById(R.id.save); spinner = (Spinner)findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dayOfWeek); adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); //retrieve saved preferences prefs = getPreferences(MODE_PRIVATE); String prefsString = prefs.getString(KEY_SavedText, null); if(prefsString != null){ editText.setText(prefsString); } int prefsInt = prefs.getInt(KEY_SavedSel, -1); if(prefsInt != -1){ spinner.setSelection(prefsInt); } //Save EditText if buttonSave clicked buttonSave.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putString(KEY_SavedText, editText.getText().toString()); editor.commit(); } }); //Save Spinner selection spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putInt(KEY_SavedSel, position); editor.commit(); } @Override public void onNothingSelected(AdapterView<?> parent) {} }); } }
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.androidsharedpreferences.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Save Text" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
May 14, 2014
Display TextView with multi-color
This example show how to display TextView with multi-color, using Html.fromHtml() and SpannableString.
TextView with multi-color |
package com.example.androidcolortext; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.text.Html; import android.text.SpannableString; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.widget.TextView; import android.widget.TextView.BufferType; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView colorText1 = (TextView)findViewById(R.id.colortext1); String text1 = "<font COLOR=\'RED\'><b>" + "android-coding" + "</b></font>" + "<font COLOR=\'#00FF00\'><i>" + ".blogspot" + "</i></font>" + ".com"; colorText1.setText(Html.fromHtml(text1)); TextView colorText2 = (TextView)findViewById(R.id.colortext2); SpannableString text2 = new SpannableString("android-coding.blogspot.com"); text2.setSpan(new ForegroundColorSpan(Color.RED), 0, 14, 0); text2.setSpan(new ForegroundColorSpan(Color.GREEN), 6, 11, 0); text2.setSpan(new ForegroundColorSpan(Color.BLUE), 15, text2.length(), 0); colorText2.setText(text2, BufferType.SPANNABLE); TextView colorText3 = (TextView)findViewById(R.id.colortext3); SpannableString text3 = new SpannableString("android-coding.blogspot.com"); text3.setSpan(new BackgroundColorSpan(Color.LTGRAY), 0, text3.length(), 0); text3.setSpan(new ForegroundColorSpan(Color.RED), 0, 14, 0); text3.setSpan(new ForegroundColorSpan(Color.GREEN), 6, 11, 0); text3.setSpan(new ForegroundColorSpan(0xFF0000FF), 14, 23, 0); text3.setSpan(new ForegroundColorSpan(0x500000FF), 23, text3.length(), 0); colorText3.setText(text3, BufferType.SPANNABLE); } }
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.androidcolortext.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <TextView android:id="@+id/colortext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="50sp" /> <TextView android:id="@+id/colortext2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="50sp" /> <TextView android:id="@+id/colortext3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="50sp" /> </LinearLayout>
May 13, 2014
ComposePathEffect vs SumPathEffect
This example show different effect of ComposePathEffect vs SumPathEffect, on CornerPathEffect and DashPathEffect.
/res/layout/activity_main.xml
MyView.java
MainActivity.java
- More examples of drawing path on custom View.
/res/layout/activity_main.xml
<LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.androidview.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="CornerPathEffect radius" /> <SeekBar android:id="@+id/radius" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="500" android:progress="30" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="DashPathEffect phase" /> <SeekBar android:id="@+id/phase" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ToggleButton android:id="@+id/combineffect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="SumPathEffect" android:textOn="ComposePathEffect" /> <RadioGroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/styleFill" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FILL" android:checked="true" /> <RadioButton android:id="@+id/styleStroke" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="STROKE" /> <RadioButton android:id="@+id/styleFillAndStroke" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FILL_AND_STROKE" /> </RadioGroup> </LinearLayout> <com.example.androidview.MyView android:id="@+id/myview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MyView.java
package com.example.androidview; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ComposePathEffect; import android.graphics.CornerPathEffect; import android.graphics.DashPathEffect; import android.graphics.Paint; import android.graphics.Path; import android.graphics.SumPathEffect; import android.graphics.Path.Direction; import android.graphics.PathEffect; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paintBorder, paintCircle; Path pathBorder, pathCircle; Path pathShape; float cornerRadius; float dashPhase; float[] intervals = {50.0f, 50.0f}; //true: ComposePathEffect //false: SumPathEffect boolean combinPathEffect; Paint.Style style = Paint.Style.FILL; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paintBorder = new Paint(); paintBorder.setColor(Color.BLUE); paintBorder.setStrokeWidth(5); paintCircle = new Paint(); paintCircle.setColor(Color.RED); paintCircle.setStrokeWidth(5); pathBorder = new Path(); pathCircle = new Path(); pathShape = new Path(); pathShape.moveTo(0, 0); pathShape.lineTo(10, 20); pathShape.lineTo(20, 0); pathShape.close(); cornerRadius = 30.0f; dashPhase = 30.0f; } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.GRAY); pathBorder.reset(); pathBorder.moveTo(50, 50); pathBorder.lineTo(50, getHeight()-50); pathBorder.lineTo(getWidth()-50, getHeight()-50); pathBorder.lineTo(getWidth()-50, 50); pathBorder.close(); float radius; pathCircle.reset(); if(getWidth()>getHeight()){ radius = getHeight()/4; }else{ radius = getWidth()/4; } pathCircle.addCircle(getWidth()/2, getHeight()/2, radius, Direction.CCW); CornerPathEffect cornerPathEffect = new CornerPathEffect(cornerRadius); DashPathEffect dashPathEffect = new DashPathEffect(intervals, dashPhase); PathEffect pathEffect; if(combinPathEffect){ pathEffect = new ComposePathEffect(dashPathEffect, cornerPathEffect); }else{ pathEffect = new SumPathEffect(dashPathEffect, cornerPathEffect); } paintBorder.setStyle(style); paintCircle.setStyle(style); paintBorder.setPathEffect(pathEffect); paintCircle.setPathEffect(pathEffect); canvas.drawPath(pathBorder, paintBorder); canvas.drawPath(pathCircle, paintCircle); } public void setCornerRadius(float r){ cornerRadius = r; invalidate(); } public void setDashPhase(float p){ dashPhase = p; invalidate(); } public void setCombinPathEffect(boolean e){ combinPathEffect = e; invalidate(); } public void setStyle(Paint.Style s){ style = s; invalidate(); } }
MainActivity.java
package com.example.androidview; import android.app.Activity; import android.graphics.Paint; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.RadioButton; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.ToggleButton; public class MainActivity extends Activity { private MyView myView; private SeekBar seekBarRadius, seekBarPhase; ToggleButton buttonEffect; RadioButton radioFill, radioStroke, radioFillAndStroke; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myView = (MyView)findViewById(R.id.myview); seekBarRadius = (SeekBar)findViewById(R.id.radius); seekBarRadius.setOnSeekBarChangeListener(seekBarRadiusChangeListener); seekBarPhase = (SeekBar)findViewById(R.id.phase); seekBarPhase.setOnSeekBarChangeListener(seekBarPhaseChangeListener); buttonEffect = (ToggleButton)findViewById(R.id.combineffect); buttonEffect.setOnCheckedChangeListener(buttonEffectCheckedChangeListener); radioFill = (RadioButton)findViewById(R.id.styleFill); radioStroke = (RadioButton)findViewById(R.id.styleStroke); radioFillAndStroke = (RadioButton)findViewById(R.id.styleFillAndStroke); radioFill.setOnCheckedChangeListener(radioOnCheckedChangeListener); radioStroke.setOnCheckedChangeListener(radioOnCheckedChangeListener); radioFillAndStroke.setOnCheckedChangeListener(radioOnCheckedChangeListener); } OnSeekBarChangeListener seekBarRadiusChangeListener = new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myView.setCornerRadius(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }; OnSeekBarChangeListener seekBarPhaseChangeListener = new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myView.setDashPhase(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }; OnCheckedChangeListener buttonEffectCheckedChangeListener = new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { myView.setCombinPathEffect(isChecked); }}; OnCheckedChangeListener radioOnCheckedChangeListener = new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(radioFill.isChecked()){ myView.setStyle(Paint.Style.FILL); }else if(radioStroke.isChecked()){ myView.setStyle(Paint.Style.STROKE); }else{ myView.setStyle(Paint.Style.FILL_AND_STROKE); } } }; }
- More examples of drawing path on custom View.
May 11, 2014
Fill path and stroke with different color
Check the demo video. The outter (deviated) rectangle drawn with Paint with style of Paint.Style.FILL_AND_STROKE, both the filled area and stroke have the same color. The inner (deviated) circle drawn in two times; first time drawn with Paint.Style.FILL to fill the inner area, the second time draw with Paint.Style.STROKE to draw the stroke with different color.
Other files, /res/layout/activity_main.xml and MainActivity.java, refer to last post DiscretePathEffect example.
- More examples of drawing path on custom View.
package com.example.androidview; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.DiscretePathEffect; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Path.Direction; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paintBorder, paintCircle; Path pathBorder, pathCircle; Path pathShape; float segmentLength; float deviation; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paintBorder = new Paint(); paintBorder.setColor(Color.BLUE); paintBorder.setStrokeWidth(20); paintBorder.setStyle(Paint.Style.FILL_AND_STROKE); paintCircle = new Paint(); paintCircle.setStrokeWidth(20); pathBorder = new Path(); pathCircle = new Path(); pathShape = new Path(); pathShape.moveTo(0, 0); pathShape.lineTo(10, 20); pathShape.lineTo(20, 0); pathShape.close(); segmentLength = 30.0f; deviation = 30.0f; } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.GRAY); pathBorder.reset(); pathBorder.moveTo(50, 50); pathBorder.lineTo(50, getHeight()-50); pathBorder.lineTo(getWidth()-50, getHeight()-50); pathBorder.lineTo(getWidth()-50, 50); pathBorder.close(); float radius; pathCircle.reset(); if(getWidth()>getHeight()){ radius = getHeight()/4; }else{ radius = getWidth()/4; } pathCircle.addCircle(getWidth()/2, getHeight()/2, radius, Direction.CCW); DiscretePathEffect discretePathEffect = new DiscretePathEffect(segmentLength, deviation); paintBorder.setPathEffect(discretePathEffect); canvas.drawPath(pathBorder, paintBorder); //fill circle with color paintCircle.setColor(Color.RED); paintCircle.setStyle(Paint.Style.FILL); paintCircle.setPathEffect(discretePathEffect); canvas.drawPath(pathCircle, paintCircle); //draw stroke with different color paintCircle.setColor(Color.BLACK); paintCircle.setStyle(Paint.Style.STROKE); paintCircle.setPathEffect(discretePathEffect); canvas.drawPath(pathCircle, paintCircle); } public void setDeviation(int dev){ deviation = (float)dev; invalidate(); } public void setSegmentLength(int seglen){ //force segmentLength not 0 if (seglen==0){ seglen = 1; } segmentLength = (float)seglen; invalidate(); } public void setStrokeWidth(int strwidth){ paintBorder.setStrokeWidth(strwidth); paintCircle.setStrokeWidth(strwidth); invalidate(); } }
Other files, /res/layout/activity_main.xml and MainActivity.java, refer to last post DiscretePathEffect example.
- More examples of drawing path on custom View.
May 10, 2014
DiscretePathEffect example
android.graphics.DiscretePathEffect chop the path into lines of segmentLength, randomly deviating from the original path by deviation.
Main layout, /res/layout/activity_main.xml.
MainActivity.java
MyView.java
- More examples of drawing path on custom View.
Main layout, /res/layout/activity_main.xml.
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.androidview.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="segmentLength"/> <SeekBar android:id="@+id/segmentLength" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="deviation"/> <SeekBar android:id="@+id/deviation" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="strokeWidth"/> <SeekBar android:id="@+id/strokeWidth" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="20"/> <com.example.androidview.MyView android:id="@+id/myview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java
package com.example.androidview; import android.app.Activity; import android.graphics.PathDashPathEffect; import android.graphics.PathDashPathEffect.Style; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Spinner; public class MainActivity extends Activity { private MyView myView; private SeekBar seekBarSegmentLength, seekBarDeviation; private SeekBar seekBarStrokeWidth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myView = (MyView)findViewById(R.id.myview); seekBarSegmentLength = (SeekBar)findViewById(R.id.segmentLength); seekBarSegmentLength.setOnSeekBarChangeListener(seekBarSegmentLengthChangeListener); seekBarDeviation = (SeekBar)findViewById(R.id.deviation); seekBarDeviation.setOnSeekBarChangeListener(seekBarDeviationChangeListener); seekBarStrokeWidth = (SeekBar)findViewById(R.id.strokeWidth); seekBarStrokeWidth.setOnSeekBarChangeListener(seekBarStrokeWidthChangeListener); } OnSeekBarChangeListener seekBarSegmentLengthChangeListener = new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myView.setSegmentLength(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }; OnSeekBarChangeListener seekBarDeviationChangeListener = new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myView.setDeviation(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }; OnSeekBarChangeListener seekBarStrokeWidthChangeListener = new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myView.setStrokeWidth(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }; }
MyView.java
package com.example.androidview; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.DiscretePathEffect; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Path.Direction; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paintBorder, paintCircle; Path pathBorder, pathCircle; Path pathShape; float segmentLength; float deviation; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paintBorder = new Paint(); paintBorder.setColor(Color.BLUE); paintBorder.setStrokeWidth(20); paintBorder.setStyle(Paint.Style.STROKE); paintCircle = new Paint(); paintCircle.setColor(Color.RED); paintCircle.setStrokeWidth(20); paintCircle.setStyle(Paint.Style.STROKE); pathBorder = new Path(); pathCircle = new Path(); pathShape = new Path(); pathShape.moveTo(0, 0); pathShape.lineTo(10, 20); pathShape.lineTo(20, 0); pathShape.close(); segmentLength = 30.0f; deviation = 30.0f; } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.GRAY); pathBorder.reset(); pathBorder.moveTo(50, 50); pathBorder.lineTo(50, getHeight()-50); pathBorder.lineTo(getWidth()-50, getHeight()-50); pathBorder.lineTo(getWidth()-50, 50); pathBorder.close(); float radius; pathCircle.reset(); if(getWidth()>getHeight()){ radius = getHeight()/4; }else{ radius = getWidth()/4; } pathCircle.addCircle(getWidth()/2, getHeight()/2, radius, Direction.CCW); DiscretePathEffect discretePathEffect = new DiscretePathEffect(segmentLength, deviation); paintBorder.setPathEffect(discretePathEffect); paintCircle.setPathEffect(discretePathEffect); canvas.drawPath(pathBorder, paintBorder); canvas.drawPath(pathCircle, paintCircle); } public void setDeviation(int dev){ deviation = (float)dev; invalidate(); } public void setSegmentLength(int seglen){ //force segmentLength not 0 if (seglen==0){ seglen = 1; } segmentLength = (float)seglen; invalidate(); } public void setStrokeWidth(int strwidth){ paintBorder.setStrokeWidth(strwidth); paintCircle.setStrokeWidth(strwidth); invalidate(); } }
- More examples of drawing path on custom View.
May 9, 2014
Effect of advance, phase, style in PathDashPathEffect
The example make PathDashPathEffect with interactive setting of advance, phase and style; such that you can know how they affect the result.
Main layout, /res/layout/activity_main.xml.
MainActivity.java
MyView.java
- Running PathDashPathEffect example
- More examples of drawing path on custom View.
Main layout, /res/layout/activity_main.xml.
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.androidview.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="advance"/> <SeekBar android:id="@+id/advance" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="phase"/> <SeekBar android:id="@+id/phase" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="30"/> <Spinner android:id="@+id/style" android:layout_width="match_parent" android:layout_height="wrap_content"/> <com.example.androidview.MyView android:id="@+id/myview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java
package com.example.androidview; import android.app.Activity; import android.graphics.PathDashPathEffect; import android.graphics.PathDashPathEffect.Style; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Spinner; public class MainActivity extends Activity { private MyView myView; private SeekBar seekBarAdvance, seekBarPhase; private Spinner spinnerStyle; private String[] styleNames ={ "PathDashPathEffect.Style.MORPH", "PathDashPathEffect.Style.ROTATE", "PathDashPathEffect.Style.TRANSLATE"}; private Style[] styleSettings = { PathDashPathEffect.Style.MORPH, PathDashPathEffect.Style.ROTATE, PathDashPathEffect.Style.TRANSLATE}; private ArrayAdapter<String> spinnerStyleAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myView = (MyView)findViewById(R.id.myview); seekBarAdvance = (SeekBar)findViewById(R.id.advance); seekBarAdvance.setOnSeekBarChangeListener(seekBarAdvanceChangeListener); seekBarPhase = (SeekBar)findViewById(R.id.phase); seekBarPhase.setOnSeekBarChangeListener(seekBarPhaseChangeListener); spinnerStyle = (Spinner)findViewById(R.id.style); spinnerStyleAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, styleNames); spinnerStyleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerStyle.setAdapter(spinnerStyleAdapter); spinnerStyle.setOnItemSelectedListener(spinnerStyleOnItemSelectedListener); } OnSeekBarChangeListener seekBarAdvanceChangeListener = new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myView.setAdvance(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }; OnSeekBarChangeListener seekBarPhaseChangeListener = new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { myView.setPhase(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }; OnItemSelectedListener spinnerStyleOnItemSelectedListener = new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { myView.setStype(styleSettings[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }; }
MyView.java
package com.example.androidview; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PathDashPathEffect; import android.graphics.Path.Direction; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint; Path pathBorder, pathCircle; Path pathShape; float phase; float advance; PathDashPathEffect.Style style; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.STROKE); pathBorder = new Path(); pathCircle = new Path(); pathShape = new Path(); pathShape.moveTo(0, 0); pathShape.lineTo(10, 20); pathShape.lineTo(20, 0); pathShape.close(); phase = 30.0f; advance = 30.0f; style = PathDashPathEffect.Style.MORPH; } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.GRAY); pathBorder.reset(); pathBorder.moveTo(50, 50); pathBorder.lineTo(50, getHeight()-50); pathBorder.lineTo(getWidth()-50, getHeight()-50); pathBorder.lineTo(getWidth()-50, 50); pathBorder.close(); float radius; pathCircle.reset(); if(getWidth()>getHeight()){ radius = getHeight()/4; }else{ radius = getWidth()/4; } pathCircle.addCircle(getWidth()/2, getHeight()/2, radius, Direction.CCW); PathDashPathEffect pathDashPathEffect = new PathDashPathEffect(pathShape, advance, phase, style); paint.setPathEffect(pathDashPathEffect); canvas.drawPath(pathCircle, paint); canvas.drawPath(pathBorder, paint); } public void setAdvance(int adv){ advance = (float)adv; invalidate(); } public void setPhase(int ph){ phase = (float)ph; invalidate(); } public void setStype(PathDashPathEffect.Style sty){ style = sty; invalidate(); } }
- Running PathDashPathEffect example
- More examples of drawing path on custom View.
May 8, 2014
Running PathDashPathEffect example
This example implement running PathDashPathEffect, by varying phase parameter.
Modify MyView.java from the post.
- Effect of advance, phase, style in PathDashPathEffect
- More examples of drawing path on custom View
Modify MyView.java from the post.
package com.example.androidview; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Path.Direction; import android.graphics.PathDashPathEffect; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint; Path path; Path pathShape; float phase; float advance; PathDashPathEffect.Style style; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.STROKE); path = new Path(); pathShape = new Path(); pathShape.addCircle(10, 10, 10, Direction.CCW); phase = 0; advance = 30.0f; style = PathDashPathEffect.Style.ROTATE; } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.GRAY); path.reset(); path.moveTo(50, 50); path.lineTo(50, getHeight()-50); path.lineTo(getWidth()-50, getHeight()-50); path.lineTo(getWidth()-50, 50); path.close(); phase++; PathDashPathEffect pathDashPathEffect = new PathDashPathEffect(pathShape, advance, phase, style); paint.setPathEffect(pathDashPathEffect); canvas.drawPath(path, paint); invalidate(); } }
- Effect of advance, phase, style in PathDashPathEffect
- More examples of drawing path on custom View
May 7, 2014
PathDashPathEffect example
android.graphics.PathDashPathEffect dash the drawn path by stamping it with the specified shape.
Example:
Modify MyView.java from the post.
Example:
PathDashPathEffect example |
Modify MyView.java from the post.
package com.example.androidview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Path.Direction; import android.graphics.PathDashPathEffect; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint; Path path; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.STROKE); path = new Path(); Path pathShape = new Path(); pathShape.addCircle(10, 10, 10, Direction.CCW); float advance = 30.0f; float phase = 20.0f; PathDashPathEffect.Style style = PathDashPathEffect.Style.ROTATE; PathDashPathEffect pathDashPathEffect = new PathDashPathEffect(pathShape, advance, phase, style); paint.setPathEffect(pathDashPathEffect); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.GRAY); path.reset(); path.moveTo(50, 50); path.lineTo(50, getHeight()-50); path.lineTo(getWidth()-50, getHeight()-50); path.lineTo(getWidth()-50, 50); path.close(); canvas.drawPath(path, paint); } }
May 6, 2014
ComposePathEffect example
android.graphics.ComposePathEffect create PathEffect from other two PathEffects, to apply first the inner effect and the outer pathEffect.
Here is a example to create ComposePathEffect from CornerPathEffect and DashPathEffect, in various combination.
Where:
Modify MyView.java from the post.
Here is a example to create ComposePathEffect from CornerPathEffect and DashPathEffect, in various combination.
Where:
- paint0: no effect
- paint1: CornerPathEffect
- paint2: DashPathEffect
- paint3: ComposePathEffect(cornerPathEffect, dashPathEffect)
- paint4: ComposePathEffect(dashPathEffect, cornerPathEffect)
- paint5: ComposePathEffect(cornerPathEffect, cornerPathEffect)
- ComposePathEffect(another dashPathEffect, dashPathEffect)
Modify MyView.java from the post.
package com.example.androidview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ComposePathEffect; import android.graphics.CornerPathEffect; import android.graphics.DashPathEffect; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint0, paint1, paint2, paint3, paint4, paint5, paint6; Path path; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint0 = new Paint(); paint0.setColor(Color.BLACK); paint0.setStrokeWidth(10); paint0.setStyle(Paint.Style.STROKE); paint1 = new Paint(); paint1.set(paint0); paint1.setColor(Color.RED); paint2 = new Paint(); paint2.set(paint0); paint2.setColor(Color.GREEN); paint3 = new Paint(); paint3.set(paint0); paint3.setColor(Color.BLUE); paint4 = new Paint(); paint4.set(paint0); paint4.setColor(Color.CYAN); paint5 = new Paint(); paint5.set(paint0); paint5.setColor(Color.MAGENTA); paint6 = new Paint(); paint6.set(paint0); paint6.setColor(Color.GRAY); path = new Path(); path.moveTo(20, 20); path.lineTo(20, 60); path.lineTo(100, 80); path.lineTo(10, 150); path.lineTo(20, 200); path.lineTo(20, 500); path.lineTo(100, 500); path.lineTo(100, 300); path.lineTo(150, 300); float radius = 50.0f; CornerPathEffect cornerPathEffect = new CornerPathEffect(radius); float[] intervals = new float[]{80.0f, 30.0f}; float phase = 0; DashPathEffect dashPathEffect = new DashPathEffect(intervals, phase); ComposePathEffect composePathEffect3 = new ComposePathEffect(cornerPathEffect, dashPathEffect); ComposePathEffect composePathEffect4 = new ComposePathEffect(dashPathEffect, cornerPathEffect); ComposePathEffect composePathEffect5 = new ComposePathEffect(cornerPathEffect, cornerPathEffect); float[] intervals6 = new float[]{5.0f, 5.0f}; float phase6 = 0; DashPathEffect dashPathEffect6 = new DashPathEffect(intervals6, phase6); ComposePathEffect composePathEffect6 = new ComposePathEffect(dashPathEffect6, dashPathEffect); paint1.setPathEffect(cornerPathEffect); paint2.setPathEffect(dashPathEffect); paint3.setPathEffect(composePathEffect3); paint4.setPathEffect(composePathEffect4); paint5.setPathEffect(composePathEffect5); paint6.setPathEffect(composePathEffect6); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path, paint0); //no effect canvas.translate(150, 0); canvas.drawPath(path, paint1); canvas.translate(150, 0); canvas.drawPath(path, paint2); canvas.translate(150, 0); canvas.drawPath(path, paint3); canvas.translate(150, 0); canvas.drawPath(path, paint4); canvas.translate(150, 0); canvas.drawPath(path, paint5); canvas.translate(150, 0); canvas.drawPath(path, paint6); } }
May 5, 2014
Draw rounded corner line along path, with CornerPathEffect.
Example to draw rounded corner line along path, by applying CornerPathEffect on path.
Modify MyView.java from the post.
Modify MyView.java from the post.
package com.example.androidview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.CornerPathEffect; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint; Path path; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(10); paint.setStyle(Paint.Style.STROKE); path = new Path(); path.moveTo(50, 50); path.lineTo(50, 500); path.lineTo(200, 500); path.lineTo(200, 300); path.lineTo(350, 300); float radius = 50.0f; CornerPathEffect cornerPathEffect = new CornerPathEffect(radius); paint.setPathEffect(cornerPathEffect); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path, paint); } }
May 4, 2014
Draw dash line along path, with DashPathEffect.
This example draw dash line along path, with DashPathEffect.
Modify MyView.java from last post.
Modify MyView.java from last post.
package com.example.androidview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.DashPathEffect; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint; Path path; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(10); paint.setStyle(Paint.Style.STROKE); path = new Path(); path.moveTo(50, 50); path.lineTo(50, 500); path.lineTo(200, 500); path.lineTo(200, 300); path.lineTo(350, 300); float[] intervals = new float[]{50.0f, 20.0f}; float phase = 0; DashPathEffect dashPathEffect = new DashPathEffect(intervals, phase); paint.setPathEffect(dashPathEffect); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path, paint); } }
Draw path on custom View
This example simple draw line on custom View, along path.
MyView.java, our custom View.
activity_main.xml, layout file with MyView.
MainActivity.java
More example of drawing on path:
MyView.java, our custom View.
package com.example.androidview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint; Path path; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(10); paint.setStyle(Paint.Style.STROKE); path = new Path(); path.moveTo(50, 50); path.lineTo(50, 500); path.lineTo(200, 500); path.lineTo(200, 300); path.lineTo(350, 300); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path, paint); } }
activity_main.xml, layout file with MyView.
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.androidview.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" /> <com.example.androidview.MyView android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
MainActivity.java
package com.example.androidview; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
More example of drawing on path:
- Draw dash line using DashPathEffect
- Draw rounded corner line, with CornerPathEffect
- ComposePathEffect example
- PathDashPathEffect example
- Running PathDashPathEffect example
- Effect of advance, phase, style in PathDashPathEffect
- DiscretePathEffect example
- Fill path and stroke with different color
- ComposePathEffect vs SumPathEffect
May 3, 2014
The Faster Android Emulator, Genymotion
Genymotion is an emulator using x86 architecture virtualization, making it much more efficient!
Genymotion, Android emulator
Genymotion is the next generation of the AndroVM open source project, already trusted by 300,000 developers.
It's even easier to use and has lots more functionalities.
http://www.genymotion.com/
Genymotion, Android emulator
Genymotion is the next generation of the AndroVM open source project, already trusted by 300,000 developers.
It's even easier to use and has lots more functionalities.
http://www.genymotion.com/
Subscribe to:
Posts (Atom)