1616
1717package com .mta .tehreer .demo ;
1818
19+ import android .content .Context ;
1920import android .os .Bundle ;
2021import android .view .LayoutInflater ;
2122import android .view .View ;
2223import android .view .ViewGroup ;
2324import android .widget .AdapterView ;
25+ import android .widget .ArrayAdapter ;
2426import android .widget .BaseAdapter ;
2527import android .widget .ListView ;
2628import android .widget .SeekBar ;
3941import java .util .List ;
4042
4143public class VariableFontsActivity extends AppCompatActivity {
44+ private Spinner mVariableInstanceSpinner ;
4245 private TLabel mPreviewLabel ;
46+
47+ private FontFile mFontFile ;
4348 private Typeface mTypeface ;
4449 private float [] mVariationCoordinates ;
4550
4651 private AxisAdapter mAxisAdapter = new AxisAdapter ();
4752 private List <VariationAxis > mVariationAxes = Collections .emptyList ();
4853
54+ private static class InstanceAdapter extends ArrayAdapter <Typeface > {
55+ public InstanceAdapter (Context context , FontFile fontFile ) {
56+ super (context , android .R .layout .simple_spinner_item , fontFile .getTypefaces ());
57+ setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
58+ }
59+
60+ public String getName (int position ) {
61+ Typeface typeface = getItem (position );
62+ return typeface .getStyleName ();
63+ }
64+
65+ @ Override
66+ public View getView (int position , View convertView , ViewGroup parent ) {
67+ TextView textView = (TextView ) super .getView (position , convertView , parent );
68+ textView .setText (getName (position ));
69+
70+ return textView ;
71+ }
72+
73+ @ Override
74+ public View getDropDownView (int position , View convertView , ViewGroup parent ) {
75+ TextView textView = (TextView ) super .getDropDownView (position , convertView , parent );
76+ textView .setText (getName (position ));
77+
78+ return textView ;
79+ }
80+ }
81+
4982 private class AxisAdapter extends BaseAdapter {
5083 @ Override
5184 public int getCount () {
@@ -86,16 +119,14 @@ public View getView(final int position, View convertView, ViewGroup parent) {
86119 @ Override
87120 public void onProgressChanged (SeekBar seekBar , int i , boolean b ) {
88121 float newValue = mVariationAxes .get (position ).minValue () + (i / 10.0f );
89- onCoordinateValueChanged (position , newValue );
122+ setupCoordinate (position , newValue );
90123 }
91124
92125 @ Override
93- public void onStartTrackingTouch (SeekBar seekBar ) {
94- }
126+ public void onStartTrackingTouch (SeekBar seekBar ) { }
95127
96128 @ Override
97- public void onStopTrackingTouch (SeekBar seekBar ) {
98- }
129+ public void onStopTrackingTouch (SeekBar seekBar ) { }
99130 });
100131
101132 return convertView ;
@@ -112,22 +143,28 @@ protected void onCreate(Bundle savedInstanceState) {
112143 actionBar .setDisplayHomeAsUpEnabled (true );
113144 }
114145
146+ mVariableInstanceSpinner = findViewById (R .id .spinner_variable_instance );
147+ mVariableInstanceSpinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
148+ @ Override
149+ public void onItemSelected (AdapterView <?> adapterView , View view , int i , long l ) {
150+ InstanceAdapter instanceAdapter = (InstanceAdapter ) adapterView .getAdapter ();
151+ setupTypeface (instanceAdapter .getItem (i ));
152+ }
153+
154+ @ Override
155+ public void onNothingSelected (AdapterView <?> adapterView ) { }
156+ });
157+
115158 Spinner variableFontSpinner = findViewById (R .id .spinner_variable_font );
116159 variableFontSpinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
117160 @ Override
118161 public void onItemSelected (AdapterView <?> adapterView , View view , int i , long l ) {
119162 VariableFontAdapter fontAdapter = (VariableFontAdapter ) adapterView .getAdapter ();
120- FontFile fontFile = fontAdapter .getFontFile (i );
121- mTypeface = fontFile .getTypefaces ().get (0 );
122- mVariationAxes = mTypeface .getVariationAxes ();
123- mVariationCoordinates = mTypeface .getVariationCoordinates ();
124- mAxisAdapter .notifyDataSetChanged ();
125- mPreviewLabel .setTypeface (mTypeface );
163+ setupFont (fontAdapter .getFontFile (i ));
126164 }
127165
128166 @ Override
129- public void onNothingSelected (AdapterView <?> adapterView ) {
130- }
167+ public void onNothingSelected (AdapterView <?> adapterView ) { }
131168 });
132169 variableFontSpinner .setAdapter (new VariableFontAdapter (this ));
133170 variableFontSpinner .setSelection (0 );
@@ -144,9 +181,25 @@ public boolean onSupportNavigateUp(){
144181 return true ;
145182 }
146183
147- private void onCoordinateValueChanged (int axisIndex , float coordinate ) {
148- mVariationCoordinates [axisIndex ] = coordinate ;
149- mTypeface = mTypeface .deriveVariation (mVariationCoordinates );
184+ private void setupFont (FontFile fontFile ) {
185+ mFontFile = fontFile ;
186+ mTypeface = fontFile .getTypefaces ().get (0 );
187+ mVariationAxes = mTypeface .getVariationAxes ();
188+ mVariationCoordinates = mTypeface .getVariationCoordinates ();
189+ mAxisAdapter .notifyDataSetChanged ();
190+ mVariableInstanceSpinner .setAdapter (new InstanceAdapter (this , fontFile ));
191+ mPreviewLabel .setTypeface (mTypeface );
192+ }
193+
194+ private void setupTypeface (Typeface typeface ) {
195+ mTypeface = typeface ;
196+ mVariationCoordinates = mTypeface .getVariationCoordinates ();
197+ mAxisAdapter .notifyDataSetChanged ();
150198 mPreviewLabel .setTypeface (mTypeface );
151199 }
200+
201+ private void setupCoordinate (int axisIndex , float coordinate ) {
202+ mVariationCoordinates [axisIndex ] = coordinate ;
203+ setupTypeface (mTypeface .deriveVariation (mVariationCoordinates ));
204+ }
152205}
0 commit comments