Android custom View combination control and androidview Control

Source: Internet
Author: User

Android custom View combination control and androidview Control

First

LEDView effect.

Previously, I saw a blog using two textviews to achieve this effect. So I want to use a custom control to implement an LEDView, which can be used directly.

The combination of controls, the two textviews stacked together, and then use the digital-7.ttf font to display data, so as to achieve the LED effect. The Code is as follows:

LEDView. class

Package ione. zy. demo; import java. io. file; import java. util. calendar; import java. util. date; import java. util. timeZone; import android. annotation. suppressLint; import android. content. context; import android. content. res. assetManager; import android. graphics. typeface; import android. OS. handler; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. widget. linearLayout; import android. widget. textView; public class LEDView extends LinearLayout {private TextView timeView; private TextView bgView; private static final String FONT_DIGITAL_7 = "fonts" + File. separator + "digital-7.ttf"; private static final String DATE_FORMAT = "% 02d: % 02d: % 02d"; private static final int REFRESH_DELAY = 500; private final Handler mHandler = new Handler (); private final Runnable mTimeRefresher = new Runnable () {@ Overridepublic void run () {Calendar calendar = Calendar. getInstance (TimeZone. getTimeZone ("GMT + 8"); final Date d = new Date (); calendar. setTime (d); timeView. setText (String. format (DATE_FORMAT, calendar. get (Calendar. HOUR), calendar. get (Calendar. MINUTE), calendar. get (Calendar. SECOND); mHandler. postDelayed (this, REFRESH_DELAY) ;};@ SuppressLint ("NewApi") public LEDView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle ); init (context);} public LEDView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} public LEDView (Context context) {super (context); init (context);} private void init (Context context) {LayoutInflater layoutInflater = LayoutInflater. from (context); View view = layoutInflater. inflate (R. layout. ledview, this); timeView = (TextView) view. findViewById (R. id. ledview_clock_time); bgView = (TextView) view. findViewById (R. id. ledview_clock_bg); AssetManager assets = context. getAssets (); final Typeface font = Typeface. createFromAsset (assets, FONT_DIGITAL_7); timeView. setTypeface (font); // set the bgView font. setTypeface (font); // set the font} public void start () {mHandler. post (mTimeRefresher);} public void stop () {mHandler. removeCallbacks (mTimeRefresher );}}

Ledview. xml file

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >      <TextView          android:id ="@+id/ledview_clock_time"          android:layout_width ="wrap_content"          android:layout_height ="wrap_content"          android:layout_centerInParent="true"        android:shadowColor ="#00ff00"          android:shadowDx ="0"          android:shadowDy ="0"          android:shadowRadius ="10"          android:textColor ="#00ff00"          android:textSize ="80sp" />              <TextView        android:id ="@+id/ledview_clock_bg"           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_centerInParent="true"        android:layout_gravity="center"          android:text="@string/default_time"          android:textColor="#3300ff00"          android:textSize="80sp" /> </RelativeLayout>


Use Demo

package ione.zy.demo;import android.os.Build;import android.os.Bundle;import android.view.Menu;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.ActionBar;import android.app.Activity;@TargetApi(Build.VERSION_CODES.HONEYCOMB)public class LEDActivity extends Activity {private LEDView ledView;@SuppressLint("NewApi")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_led);ledView = (LEDView) findViewById(R.id.ledview);  ActionBar actionBar = getActionBar();    actionBar.setDisplayHomeAsUpEnabled(true); }@Overrideprotected void onResume() {super.onResume();ledView.start();}@Overrideprotected void onStop() {super.onStop();ledView.stop();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_led, menu);return true;}}


Activity_led.xml

<RelativeLayout 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"    tools:context=".LEDActivity"    android:background="@color/black" >   <ione.zy.demo.LEDView         android:id="@+id/ledview"        android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_centerInParent="true"        android:layout_gravity="center"  />  </RelativeLayout>


Download Demo



Android custom View widget

Your buttons and textview are not initialized. It is strange that you do not report a null pointer.

When does Android use custom views?

There are two types of android applications: software and game.
Generally, the software does not use custom views and uses the android built-in controls, because the android built-in controls can already meet the needs of general applications.
However, the game uses the android built-in controls to make a very good result, because the game is diversified, various characters, various items, various interfaces, and various effects. Therefore, the game uses a custom View or SurfaceView as the screen content.

In fact, it is not clear whether you want to ask this question. Are you satisfied with this answer?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.