Draw text drawText Android basic example
drawText(), setColor(), setTextSize(), setTextAlign()
public class ApokusActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
paint.setTextSize(60);
//paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Hello world!", 0, 50, paint);
}
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Codec, compress(), CompressFormat, quality, decodeByteArray(), Android example source code with image.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
src.compress(format, quality, os);
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.GRAY);
// you need to insert some image flower_blue into res/drawable folder
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
// Best of quality is 80 and more, 3 is very low quality of image
Bitmap bJPGcompress = codec(b, Bitmap.CompressFormat.JPEG, 3);
// get dimension of bitmap getHeight() getWidth()
int h = b.getHeight();
canvas.drawBitmap(b, 10,10, paint);
canvas.drawBitmap(bJPGcompress, 10,10 + h + 10, paint);
}
}
}
String[] source = {"Hello","world","by","Android"};
String[] destination = new String[source.length];
System.arraycopy(source, 0, destination, 0, source.length);
Problemi is in e.getMessage() what can return null and Log.e (String tag, String msg) will throws an new exception !!!!
Problem and solution:
LogCat:
Or you can using this code:
Problem and solution:
try {
int [] i = {1};
int z = i[5];
} catch (ArrayIndexOutOfBoundsException e) {
String s = e.toString(); // s == java.lang.ArrayIndexOutOfBoundsException
// try to testing String s for null value
if(s != null)
Log.e("bla", s);
else
Log.e("bla", "My error text 1");
String s2 = e.getMessage(); // s2 == null !!!!!!!
// you need to testing String s2 for null value , or you get FATAL EXCEPTION: main
// and application will be crashed
String s2 = e.getMessage(); // s2 == null !!!!!!!
if(s2 != null)
Log.e("bla2", e.getMessage());
else
Log.e("bla2", "My error text 2");
// this is OK
e.printStackTrace();
}
LogCat:
E/bla(855): java.lang.ArrayIndexOutOfBoundsException
E/bla2(855): My error text 2
W/System.err(855): java.lang.ArrayIndexOutOfBoundsException
W/System.err(855): at cz.okhelp.motion._MotionActivity.onTouchEvent(_MotionActivity.java:54)
W/System.err(855): at android.app.Activity.dispatchTouchEvent(Activity.java:2099)
W/System.err(855): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
W/System.err(855): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
W/System.err(855): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
W/System.err(855): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(855): at android.os.Looper.loop(Looper.java:123)
W/System.err(855): at android.app.ActivityThread.main(ActivityThread.java:3683)
W/System.err(855): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(855): at java.lang.reflect.Method.invoke(Method.java:507)
W/System.err(855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
W/System.err(855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
W/System.err(855): at dalvik.system.NativeStart.main(Native Method)
Or you can using this code:
try {
int [] i = {1};
int z = i[5];
} catch (Exception e) {
StringBuilder sb = new StringBuilder().append(e.getClass().getSimpleName());
if (e.getMessage() != null) {
sb.append("
");
sb.append(e.getMessage());
}
Log.e("err", sb.toString()); // E/err(336): ArrayIndexOutOfBoundsException
// this code write out all message
Log.e("myError", "methodName", e);
}
// E/myError(371): methodName
// E/myError(371): java.lang.ArrayIndexOutOfBoundsException
// E/myError(371): at cz.okhelp.motion._MotionActivity.onTouchEvent(_MotionActivity.java:54)
// E/myError(371): at android.app.Activity.dispatchTouchEvent(Activity.java:2099)
// E/myError(371): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
// E/myError(371): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
// E/myError(371): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
// E/myError(371): at android.os.Handler.dispatchMessage(Handler.java:99)
// E/myError(371): at android.os.Looper.loop(Looper.java:123)
// E/myError(371): at android.app.ActivityThread.main(ActivityThread.java:3683)
// E/myError(371): at java.lang.reflect.Method.invokeNative(Native Method)
// E/myError(371): at java.lang.reflect.Method.invoke(Method.java:507)
// E/myError(371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
// E/myError(371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
// E/myError(371): at dalvik.system.NativeStart.main(Native Method)
Set in AndroidManifest.xml android:theme="@android:style/Theme.NoTitleBar" AndroidManifest.xml example source code.
AndroidManifest.xml
AndroidManifest.xml
<manifest xmlns:android="//schemas.android.com/apk/res/android"
package="com.myexample.without_titlebar">
<application android:label="My app">
<activity android:name="NoTitleBar"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Issue: Cropped superscript index between tags sup /sup is not correctly visible in TextView or View as Button.
String s = "10<sup>12 </sup>";
textView.setText(Html.fromHtml(s)); // 12 will cropped
// solution:
s = "10<sup>12 </sup>\t "; // add behind ending of sup tag the tabulator \t,
// but not char \t but only press to TAB key!!! in source code
textView.setText(Html.fromHtml(s)); // 12 is visible correctly
Editace: 2013-12-09 13:10:42
Počet článků v kategorii: 396
Url:draw-text-drawtext-android-basic-example