Tuesday, March 12, 2019

Android开发笔记-ch3.4.10 Rolling Text, Overlay & SurfaceView, Toast image, Animation


3.4.10 Rolling Text, Overlay & SurfaceView, Toast image, Animation

Like the caption for video playback. Need do some research. Several refs: SO1, SO2, SO3, are mostly for text over ImageView.
As mention above, using MediaPlayer can combine with SurfaceView. Refer to this SO and Android Graphics. As this blog said: We don't use View directly but use its sub-class such as ImageView, TextView or customize View. Those subclass are called widget. SurfaceView have three primary usages: video playback, camera preview and 2D game. SurfaceView contains a SurfaceHolder which you can pass to MediaPlayer or Camera as display sink. (Another option is to pass a TextureView – which deserves a article for explaining). MediaPlayer/Camera don't accept widget as display sink. surfaceView can not be hardware accelerated (as of JB4.2) while 95% operations on normal View are HW accelerated using openGL ES. Others mentioned that SurfaceView can be drawn on by background theads but Views can't. SurfaceView use more resources though so you don't want to use them unless you have to.
The HTML <marquee> element is used to insert a scrolling area of text. It is obsolete. It can be used for TextView in android:
Refer to this SO: probabily needs all three code and the text has to be longer than the view width:
tv.setSelected(true);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSingleLine(true);
Toast.makeText is commonly used. It is also possible to toast an ImageView as explained in this SO and toasts, just like this:
    Toast toast = new Toast(getApplicationContext());
ImageView view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.your_img);
toast.setView(view);
toast.show();
Note there are serveral approach in the SO; Above code does work, but w/o a layout, the image size vary on different device and is not controllable even with setLayoutParams. Should create a LinearLayout or RelativeLayout with ImageView, then call setView(layout):
LinearLayout layout = new LinearLayout(getApplicationContext());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
layout.addView(imgView, size.x, size.y/6); //display a banner imgView at bottom 1/6 of the screen
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Note the duration cannot be changed from the two default setting as explained in SO. The long is 3.5 second and the short is 2 second. Repeat calling Toast may increase the duration, but may also causing flicker. One solution is:
final Toast tag = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);
new CountDownTimer(9000, 1000)
{
    public void onTick(long millisUntilFinished) {tag.show();}
    public void onFinish() {tag.show();}
}.start();
As explained, repeating calling Toast.show has no side effect other than refresh. So you can also use timer to repeat the show.
Regarding animation, refer to this SO. The easy way would be using WebView.

0 Comments:

Post a Comment