LECTURE 4 UI ÉÁ²è¤òôÅö¤¹¤ë View ¥¯¥é¥¹

¡û ¤Ï¤¸¤á¤Ë

LECTURE 2 ʸ»úɽ¼¨¤À¤±¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó (Activity/TextView) ¤Ç¾¯¤·¿¨¤ì¤Þ¤·¤¿¤¬¡¢¼ÂºÝ¤Ë²èÌ̤òÉÁ²è¤¹¤ë¤Î¤Ï Activity ¤Ç¤Ï¤Ê¤¯ android.view.View¡Ê¤ª¤è¤Ó¥µ¥Ö¥¯¥é¥¹¡Ë¤Ç¤¹¡£android.widget ¥Ñ¥Ã¥±¡¼¥¸¤Ë¤Ï¡¢View ¥¯¥é¥¹¤ò·Ñ¾µ¤·¤Æºî¤é¤ì¤¿¿§¡¹¤Ê UI ÉôÉʤ¬ÍÑ°Õ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£Ã±°ì¤Î View ¤ò°ì²èÌ̤ˤɡ¼¤ó¤Èɽ¼¨¤¹¤ë¾¤Ë¡¢android.view.ViewGroup ¤Î¥µ¥Ö¥¯¥é¥¹¤òÍøÍѤ·¤Æ¡¢Ê£¿ô¤Î View ¤ò²èÌ̾å¤Ë¥ì¥¤¥¢¥¦¥È¤·¤Æɽ¼¨¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£

¤³¤³¤Ç¤Ï¡¢¤È¤ê¤¢¤¨¤º²èÌ̤òÉÁ²è¤¹¤ë¤¿¤á¤Î¥á¥½¥Ã¥É¤À¤±¤ò¥ª¡¼¥Ð¡¼¥é¥¤¥É¤·¤¿¥µ¥Ö¥¯¥é¥¹¤ò¼«ºî¤·¤Æ¡¢²èÌÌɽ¼¨¤Î»ÅÁȤߤÎÊ·°Ïµ¤¤òÄϤߤ¿¤¤¤È»×¤¤¤Þ¤¹¡£

¡û View ID

View ¤ò¼ÂºÝ¤Ë²èÌ̤ȷë¤Ó¤Ä¤±¤ë¤¿¤á¤Ë¤Ï¡¢LECTURE 2 ʸ»úɽ¼¨¤À¤±¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó (Activity/TextView) ¤Ë¤ÆÀâÌÀ¤·¤Æ¤¤¤ëÍͤˡ¢Activity#setContentView() ¤ò¼Â¹Ô¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£

public void Activity#setContentView(int layoutResID);
public void Activity#setContentView(android.view.View view);
public void Activity#setContentView(
  android.view.View view, android.view.ViewGroup.LayoutParams params);

setContentView() ¤Ë¤Ï¡¢View ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò»ØÄꤹ¤ë¾¤Ë¡¢¥ê¥½¡¼¥¹ ID ¤ò»ØÄꤷ¤Æ¥ê¥½¡¼¥¹ÄêµÁ¤«¤é UI ¥ì¥¤¥¢¥¦¥È¤òÆɤ߹þ¤Þ¤»¤ë¤³¤È¤â½ÐÍè¤Þ¤¹¡ÊAndroid Ū¤Ë¤Ï¸å¼Ô¤¬¿ä¾©¤ÎÊýË¡¡Ë¡£

¥ê¥½¡¼¥¹ ID »ØÄê¤Ç setContentView() ¤ò¼Â¹Ô¤¹¤ë¾ì¹ç¡¢¸å¤«¤é View ¤òÁàºî¤¹¤ë¡Ê¸å¤«¤é View ¤Ë¥×¥í¥Ñ¥Æ¥£¤òÀßÄꤷ¤¿¤ê¡¢É½¼¨/Èóɽ¼¨¤òÀÚ¤êÂؤ¨¤¿¤ê¤¹¤ëÅù¡Ë¤¿¤á¤Ë¤Ï¡¢View ¤Ø¤Î»²¾È¤òÊÌÅÓ¼èÆÀ¤·¤Ê¤±¤ì¤Ð¤Ê¤ê¤Þ¤»¤ó¡£¤³¤ì¤Ë¤Ï¡¢Activity#findViewById() ¤ò»È¤¤¤Þ¤¹¡£

public View Activity#findViewById(int id);

¤³¤Î¥á¥½¥Ã¥É¤Ë¤Ï¡¢View ¤ò¼±Ê̤¹¤ë¤¿¤á¤Î id ¤ò»ØÄꤹ¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£¤³¤Î id ¤Ï¡¢UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë¤Ë¤Æµ­½Ò¤·¤Þ¤¹¡£Î㤨¤Ð LECTURE 2 ʸ»úɽ¼¨¤À¤±¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó (Activity/TextView) ¤Î UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë¤Ï

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MainActivity"
    />
</LinearLayout>

¤Ç¤·¤¿¡£¤³¤Î¥Õ¥¡¥¤¥ë¤Ç¤Ï¡¢View ¤Î id ¤òÀßÄꤷ¤Æ¤¤¤Ê¤¤¤¿¤á¡¢¸å¤«¤é findViewById() ¤Ç View ¤Î»²¾È¤ò¼èÆÀ¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡£TextView ¤Ø¤Î»²¾È¤ò¸å¤«¤é¼èÆÀ¤Ç¤­¤ëÍͤˤ¹¤ë¤¿¤á¡¢°Ê²¼¤ÎÍÍ¤Ë id ¤òÉÕÍ¿¤·¤Þ¤¹¡£

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MainActivity"
    />
</LinearLayout>

TextView ¤Î¥¿¥°¤Ë android:id="@+id/textView" ¤È¤¤¤¦Â°À­ÀßÄê¤òÉÕÍ¿¤·¤Þ¤·¤¿¡£¤³¤ì¤Ë¤è¤ê¡¢TextView ¤ËÂФ·¤Æ "textView" ¤È¤¤¤¦ id ¤¬ÉÕ¤¤¤¿¤¿¤á¡¢¸å¤«¤é findViewById() ¤Ç»²¾È¤ò¼èÆÀ¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

    /** Called with the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView = (TextView)findViewById(R.id.textView);
        //textView.xxxx();
    }

¤Ê¤ª¡¢"@+id/..." ¤È¤¤¤¦É½¸½¤Ë¤è¤ê¡¢¿·¤·¤¤ id Ãͤò R.java ( ant»þ¤Ë¼«Æ°À¸À® ) ¤ËÍÑ°Õ¤·¤Æ¤¯¤ì¤ëÍͤˤʤê¤Þ¤¹¡Ê+ ¤¬Ìµ¤¤¤È¡¢´û¤Ë¤¢¤ë¾ì¹ç¤Ï¤½¤ì¤ò»È¤Ã¤Æ¤·¤Þ¤¦¤¿¤á¡¢id ¤È¤·¤Æ¤ÏÉÔŬÅö¡Ë¡£

¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÈÀâÌÀ

¼«ºî View ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤¹¡£004dispimage.zip ¤«¤é¥À¥¦¥ó¥í¡¼¥É¤Ç¤­¤Þ¤¹¡£¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò ¸Ç¤á¤¿ zip ¥Õ¥¡¥¤¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£

¤Ê¤ª¡¢¥½¡¼¥¹¥³¡¼¥É¤Ï SDK Tools 22.0.4 / ¥×¥é¥Ã¥È¥Õ¥©¡¼¥à Android 4.2 ¤Ç¥³¥ó¥Ñ¥¤¥ë²Äǽ¤Ç¤¹¡£local.properties Æâ¤Ë sdk.dir ÊÑ¿ô¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤Þ¤¹¤¬¡¢¤³¤³¤Ï´Ä¶­°Í¸¤Ë¤Ê¤ê¤Þ¤¹¤Î¤Ç¡¢¤ª»È¤¤¤Î´Ä¶­¤Ë¹ç¤ï¤»¤ÆÊѹ¹¤·¤Æ¤¯¤À¤µ¤¤¡£

¥½¡¼¥¹¥³¡¼¥ÉÇÛÉۤϽ¤Àµ BSD ¥é¥¤¥»¥ó¥¹¤Ë½¾¤¤¤Þ¤¹¡Ê¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤ÎÌÈÀÕ»ö¹à¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡Ë¡£

QVGA(320x480:mdpi) ¤Î¥¨¥ß¥å¥ì¡¼¥¿¤Ç¼Â¹Ô¤¹¤ë¤È¡¢¢­¤ÎÍͤËɽ¼¨¤µ¤ì¤Þ¤¹¡£
DispImage ¼Â¹Ô²èÌÌ

¤³¤³¤Ç¤Ï

  • DispImage.java
  • CustomView.java
  • dispimage.xml
  • strings.xml
  • colors.xml

¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤¹¡£

  • DispImage.java
        1	/*
        2	 * Copyright (c) 2013, Ryo Ichinose
        3	 * All rights reserved.
        4	 *
        5	 * Redistribution and use in source and binary forms, with or without 
        6	 * modification, are permitted provided that the following conditions are met:
        7	 *
        8	 *    * Redistributions of source code must retain the above copyright 
        9	 *      notice, this list of conditions and the following disclaimer.
       10	 *    * Redistributions in binary form must reproduce the above copyright 
       11	 *      notice, this list of conditions and the following disclaimer in the 
       12	 *      documentation and/or other materials provided with the distribution.
       13	 *    * Neither the name of the auther nor the 
       14	 *      names of its contributors may be used to endorse or promote products 
       15	 *      derived from this software without specific prior written permission.
       16	 *
       17	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       18	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
       19	 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
       20	 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
       21	 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
       22	 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
       23	 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       24	 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
       25	 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
       26	 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
       27	 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       28	 */
       29	
       30	package riolab.sample;
       31	
       32	import android.app.Activity;
       33	import android.os.Bundle;
       34	
       35	public class DispImage extends Activity
       36	{
       37	    /** Called when the activity is first created. */
       38	    @Override
       39	    public void onCreate(Bundle savedInstanceState)
       40	    {
       41	        super.onCreate(savedInstanceState);
       42	        setContentView(R.layout.dispimage);
       43	
       44		CustomView view = (CustomView)findViewById(R.id.customView);
       45		view.setBgColor(getResources().getColor(R.color.color_bg));
       46		view.setFgColor(getResources().getColor(R.color.color_fg));
       47	    }
       48	}
    
    
    • 42 ¹ÔÌÜ

      º£²ó¤Ï UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë¤Î̾Á°¤ò dispimage.xml ¤Ë¤·¤Þ¤·¤¿¡£¤½¤Î¤¿¤á¡¢Activity#setContentView() ¤ËÀßÄꤹ¤ë¥ê¥½¡¼¥¹ ID ¤â¤½¤ì¤ËÂбþ¤·¤Æ R.layout.dispimage ¤È¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£

    • 44 ¹ÔÌÜ

      Activity#setContentView() ¤Ç UI ¤ò¹½ÃÛ¤·¤¿¸å¤Ë¡¢º£²ó¼«ºî¤·¤¿ View ¤Ø¤Î»²¾È¤ò Activity#findViewById() ¤Ç¼èÆÀ¤·¤Þ¤¹¡£

      public View Activity#findViewById(int id);
      
      

      id ¤Ë¤Ï¡¢res/layout/dispimage.xml ¤ÇÀßÄꤷ¤¿ "customView" ¤È¤¤¤¦Ê¸»úÎó¤ò»ØÄꤷ¤Þ¤¹¡£ Ìá¤êÃͤÏÈÆÍÑŪ¤Ê View ¤Î¤¿¤á¡¢¼«ºî¤·¤¿ CustomView ¤Ë¥­¥ã¥¹¥È¤·¤Æ»È¤¤¤Þ¤¹¡£

    • 45 ¡Á 46¹ÔÌÜ

      CustomView#setBgColor() ¤ª¤è¤Ó CustomView#setFgColor() ¤ò»È¤Ã¤Æ¡¢CustomView ¤Ç»ÈÍѤ¹¤ëÁ°·Ê¿§/ÇØ·Ê¿§¤ò»ØÄꤷ¤Þ¤¹¡£¿§ÄêµÁ¤Ï¡¢android.graphics.Color ¤Î¥á¥ó¥Ð¤ò»È¤Ã¤Æ»ØÄꤹ¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¤¬¡¢º£²ó¤Ï res/values/colors.xml ¤Ë¥ê¥½¡¼¥¹ÄêµÁ¤·¤Æ¤½¤ì¤òÆɤ߹þ¤àÍͤˤ·¤Þ¤·¤¿¡£

      ¥ê¥½¡¼¥¹¤«¤é¿§ÄêµÁ¤òÆɤ߹þ¤à¤Ë¤Ï¡¢android.content.res.Resources#getColor() ¤ò»È¤¤¤Þ¤¹¡£

      public int Resources#getColor(int id);
      
      

      colors.xml ¤ËÄêµÁ¤·¤¿¥ê¥½¡¼¥¹¤Ï¡¢R.color.<¿§¼±ÊÌ̾¾Î> ¤È¤·¤Æ ID »ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

  • CustomView.java
        1	/*
        2	 * Copyright (c) 2013, Ryo Ichinose
        3	 * All rights reserved.
        4	 *
        5	 * Redistribution and use in source and binary forms, with or without 
        6	 * modification, are permitted provided that the following conditions are met:
        7	 *
        8	 *    * Redistributions of source code must retain the above copyright 
        9	 *      notice, this list of conditions and the following disclaimer.
       10	 *    * Redistributions in binary form must reproduce the above copyright 
       11	 *      notice, this list of conditions and the following disclaimer in the 
       12	 *      documentation and/or other materials provided with the distribution.
       13	 *    * Neither the name of the auther nor the 
       14	 *      names of its contributors may be used to endorse or promote products 
       15	 *      derived from this software without specific prior written permission.
       16	 *
       17	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       18	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
       19	 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
       20	 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
       21	 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
       22	 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
       23	 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       24	 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
       25	 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
       26	 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
       27	 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       28	 */
       29	
       30	package riolab.sample;
       31	
       32	import java.util.ArrayList;
       33	import java.util.List;
       34	import java.util.Map;
       35	
       36	import android.content.Context;
       37	import android.content.res.Resources;
       38	import android.graphics.Canvas;
       39	import android.graphics.Paint;
       40	import android.graphics.Paint.FontMetrics;
       41	import android.util.AttributeSet;
       42	import android.view.View;
       43	
       44	public class CustomView extends View
       45	{
       46	    private static final int TEXT_SIZE = 24;
       47	    private final Paint paint = new Paint();
       48	    private float textHeight;
       49	
       50	    private List<String> textList = new ArrayList<String>();
       51	    private int fgColor;
       52	    private int bgColor;
       53	
       54	    public CustomView(Context context)
       55	    {
       56		super(context);
       57		init();
       58	    }
       59	
       60	    public CustomView(Context context, AttributeSet attrs)
       61	    {
       62		super(context, attrs);
       63		init();
       64	    }
       65	
       66	    public CustomView(Context context, AttributeSet attrs, int defStyle)
       67	    {
       68		super(context, attrs, defStyle);
       69		init();
       70	    }
       71	
       72	    private void init()
       73	    {
       74		paint.setAntiAlias(true);
       75		paint.setTextSize(CustomView.TEXT_SIZE);
       76	
       77		FontMetrics metrics = paint.getFontMetrics();
       78		textHeight = (-metrics.top) + metrics.bottom;
       79	
       80		Resources res = getContext().getResources();
       81		textList.add(res.getString(R.string.text1));
       82		textList.add(res.getString(R.string.text2));
       83		textList.add(res.getString(R.string.text3));
       84	    }
       85	
       86	    public void setBgColor(int color)
       87	    {
       88		bgColor = color;
       89	    }
       90	
       91	    public void setFgColor(int color)
       92	    {
       93		fgColor = color;
       94		paint.setColor(fgColor);
       95	    }
       96	
       97	    @Override
       98	    public void onDraw(Canvas canvas)
       99	    {
      100		super.onDraw(canvas);
      101		canvas.drawColor(bgColor);
      102	
      103		float x = 30.0f;
      104		float y = 150.0f;
      105	
      106		for (String text : textList) {
      107		    canvas.drawText(text, x, y, paint);
      108		    y += textHeight;
      109		}
      110	    }
      111	}
    
    

    ËÜ¥µ¥ó¥×¥ë¤Î¥á¥¤¥ó¤È¤Ê¤ë¡¢¼«ºî¤Î View ¥¯¥é¥¹¤Ç¤¹¡£ÇطʤòÇØ·Ê¿§¤ÇÅɤê¤Ä¤Ö¤·¤Æ¡¢3 ¹Ô¤Îʸ»úÎó¤ò²èÌ̤ËÉÁ²è¤·¤Þ¤¹¡£

    • 44 ¹ÔÌÜ

      android.view.View ¤ò·Ñ¾µ¤·¤Æ¤¤¤Þ¤¹¡£¤³¤ì¤Ë¤è¤ê¡¢²èÌÌɽ¼¨¥¯¥é¥¹¤È¤·¤Æ»È¤¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

    • 47 ¹ÔÌÜ

      ²èÌÌÆâÍƤÎÉÁ²è»þ¤Ë»È¤¦¡¢Àþ¼ï¤ä¥Õ¥©¥ó¥È¥µ¥¤¥ºÅù¤ÎÉÁ²è°À­¤ò³ÊǼ¤¹¤ë¤¿¤á¤Î android.graphics.Paint ¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Æ¤¤¤Þ¤¹¡£SDK ¤ËÉÕ°¤·¤Æ¤¤¤ë¥µ¥ó¥×¥ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò»²¹Í¤Ë¤·¤Æ¡¢final ¤Ë¤·¤Æ¤¢¤ê¤Þ¤¹¡£

    • 54 ¡Á 70 ¹ÔÌÜ

      ¥³¥ó¥¹¥È¥é¥¯¥¿¤Ç¤¹¡£¥×¥í¥°¥é¥àÃæ¤ÇľÀÜ View ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤¹¤ë¾ì¹ç¤ÏÁ°¼Ô¤Î¤ß¤Ç¹½¤¤¤Þ¤»¤ó¤¬¡¢UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë¤òÆɤ߹þ¤ó¤Ç UI ¤ò¹½ÃÛ¤¹¤ë¾ì¹ç¤Ï¡¢¸å¼Ô¤Î·Á¼°¤Î¥³¥ó¥¹¥È¥é¥¯¥¿¤¬É¬Íפˤʤê¤Þ¤¹¡£

    • 72 ¡Á 84 ¹ÔÌÜ (init())

      ½é´ü²½½èÍý¤ò¹Ô¤¤¤Þ¤¹¡£¤Þ¤º¡¢ÉÁ²è°À­¤ò³ÊǼ¤¹¤ë Paint ¥¤¥ó¥¹¥¿¥ó¥¹¤Ë¡¢¥¢¥ó¥Á¥¨¥¤¥ê¥¢¥¹¤ÎÍ­¸ú²½¤ª¤è¤Ó¥Õ¥©¥ó¥È¥µ¥¤¥º¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£Â¾¤Ë¤âÍÍ¡¹¤ÊÉÁ²è°À­¤òÀßÄê¤Ç¤­¤Þ¤¹¤Î¤Ç¡¢API ¥É¥­¥å¥á¥ó¥È¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£

      ¼¡¤Ë¡¢Paint ¤«¤é¼èÆÀ¤Ç¤­¤ë android.graphics.Paint.FontMetrics ¤ò»È¤¤¡¢Ê¸»úÎó¤òÉÁ²è¤¹¤ë¾ì¹ç¤Îʸ»ú¹â¤µ¤òµá¤á¤Æ¤¤¤Þ¤¹¡£FontMetrics#top ¤ª¤è¤Ó FontMetrics#bottom ¤Ï¤½¤ì¤¾¤ì¡¢Ê¸»ú¤Î¥Ù¡¼¥¹¥é¥¤¥ó¤È¸Æ¤Ð¤ì¤ë°ÌÃÖ¤«¤é¾å/²¼Êý¸þ¤ÎŤµ¤òɽ¤·¤Æ¤¤¤Þ¤¹¡£¤¿¤À¤·¡¢top ¤ÎÊý¤Ï²èÌ̤ΠY ºÂɸ¤¬¸º¤ëÊý¸þ¤ÎŤµ¤òɽ¤¹¤¿¤á¤«¥Þ¥¤¥Ê¥¹¤ÎÃͤˤʤäƤ¤¤Þ¤¹¤Î¤Ç¡¢È¿Å¾¤·¤Æ¤«¤é bottom ¤È²Ã»»¤·¤Æ¤¤¤Þ¤¹¡£

      ºÇ¸å¤Ë¡¢Ê¸»úÎó¥ê¥½¡¼¥¹ÄêµÁ¤«¤éʸ»úÎó¥ê¥½¡¼¥¹¤ò¼èÆÀ¤·¤Æ¤¤¤Þ¤¹¡£¥ê¥½¡¼¥¹¤«¤éʸ»úÎóÄêµÁ¤òÆɤ߹þ¤à¤Ë¤Ï¡¢android.content.res.Resources#getString()/Resources#getText() ¤ò»È¤¤¤Þ¤¹¡£

      public String Resources#getString(int id);
      public CharSequence Resources#getText(int id);
      
      

      strings.xml ¤ËÄêµÁ¤·¤¿¥ê¥½¡¼¥¹¤Ï¡¢R.string.<ʸ»úÎó¼±ÊÌ̾¾Î> ¤È¤·¤Æ ID »ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

    • 86 ¡Á 89 ¹ÔÌÜ (setBgColor())

      ÇØ·Ê¿§¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£

    • 91 ¡Á 95 ¹ÔÌÜ (setFgColor())

      Á°·Ê¿§¤òÀßÄꤹ¤ëºÝ¤Ë¡¢Æ±»þ¤Ë Paint ¤ËÂФ·¤Æ¤âÁ°·Ê¤Îɽ¼¨¿§¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£

    • 97 ¡Á 110 ¹ÔÌÜ (onDraw())

      ¼ÂºÝ¤Ë²èÌÌɽ¼¨ÆâÍƤòÉÁ²è¤¹¤ë¥á¥½¥Ã¥É¤Ç¤¹¡£View ¥¯¥é¥¹¤Î¥á¥½¥Ã¥É¤ò¥ª¡¼¥Ð¥é¥¤¥É¤·¤Æ¤¤¤Þ¤¹¡£¤Þ¤º¡¢android.graphics.Canvas#drawColor() ¤ò»È¤Ã¤Æ¡¢²èÌÌÁ´ÂΤò»ØÄ꿧¤ÇÅɤê¤Ä¤Ö¤·¤Æ¤¤¤Þ¤¹¡£

      public void Canvas#drawColor(int color);
      
      

      106 ¡Á 109 ¹ÔÌܤǡ¢ÀßÄꤵ¤ì¤¿Á´¤Æ¤Îʸ»úÎó¤ò 1 ¹Ô¤Ë 1 ʸ»úÎ󤺤ÄÉÁ²è¤·¤Æ¤¤¤Þ¤¹¡£Ê¸»úÎó¤ÎÉÁ²è¤Ë¤Ï¡¢android.graphics.Canvas#drawText() ¤ò»È¤¤¤Þ¤¹¡£

      public void Canvas#drawText(String text, float x, float y, Paint paint);
      public void Canvas#drawText(
          CharSequence text, int start, int end, float x, float y, Paint paint);
      
      
  • dispimage.xml
        1	<?xml version="1.0" encoding="utf-8"?>
        2	<!--
        3	 * Copyright (c) 2013, Ryo Ichinose
        4	 * All rights reserved.
        5	 *
        6	 * Redistribution and use in source and binary forms, with or without 
        7	 * modification, are permitted provided that the following conditions are met:
        8	 *
        9	 *    * Redistributions of source code must retain the above copyright 
       10	 *      notice, this list of conditions and the following disclaimer.
       11	 *    * Redistributions in binary form must reproduce the above copyright 
       12	 *      notice, this list of conditions and the following disclaimer in the 
       13	 *      documentation and/or other materials provided with the distribution.
       14	 *    * Neither the name of the auther nor the 
       15	 *      names of its contributors may be used to endorse or promote products 
       16	 *      derived from this software without specific prior written permission.
       17	 *
       18	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       19	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
       20	 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
       21	 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
       22	 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
       23	 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
       24	 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       25	 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
       26	 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
       27	 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
       28	 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       29	-->
       30	
       31	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       32	    android:orientation="vertical"
       33	    android:layout_width="fill_parent"
       34	    android:layout_height="fill_parent"
       35	    >
       36	<riolab.sample.CustomView android:id="@+id/customView" 
       37	    android:layout_width="fill_parent" 
       38	    android:layout_height="fill_parent" 
       39	    />
       40	</LinearLayout>
    

    ËÜ¥µ¥ó¥×¥ë¤Î UI ¥ì¥¤¥¢¥¦¥ÈÄêµÁ¥Õ¥¡¥¤¥ë¤Ç¤¹¡£res/layout/dispimage.xml ¤È¤·¤ÆÊݸ¤·¤Þ¤¹¡£36 ¹ÔÌܤˤơ¢Activity ¤Ç findViewById() ¤ò»È¤Ã¤Æ View ¤Ø¤Î»²¾È¤ò¼èÆÀ¤¹¤ë¤¿¤á¡¢CustomView ¤ËÂФ·¤Æ "customView" ¤È¤¤¤¦ id ¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£

  • strings.xml
        1	<?xml version="1.0" encoding="utf-8"?>
        2	<!--
        3	 * Copyright (c) 2013, Ryo Ichinose
        4	 * All rights reserved.
        5	 *
        6	 * Redistribution and use in source and binary forms, with or without 
        7	 * modification, are permitted provided that the following conditions are met:
        8	 *
        9	 *    * Redistributions of source code must retain the above copyright 
       10	 *      notice, this list of conditions and the following disclaimer.
       11	 *    * Redistributions in binary form must reproduce the above copyright 
       12	 *      notice, this list of conditions and the following disclaimer in the 
       13	 *      documentation and/or other materials provided with the distribution.
       14	 *    * Neither the name of the auther nor the 
       15	 *      names of its contributors may be used to endorse or promote products 
       16	 *      derived from this software without specific prior written permission.
       17	 *
       18	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       19	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
       20	 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
       21	 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
       22	 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
       23	 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
       24	 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       25	 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
       26	 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
       27	 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
       28	 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       29	-->
       30	
       31	<resources>
       32	    <string name="app_name">DispImage</string>
       33	    <string name="text1">This is a</string>
       34	    <string name="text2">sample application</string>
       35	    <string name="text3">using original View class.</string>
       36	</resources>
    

    ʸ»úÎó¥ê¥½¡¼¥¹ÄêµÁ¥Õ¥¡¥¤¥ë¤Ç¤¹¡£¥Ç¥Õ¥©¥ë¥ÈÄêµÁ¤Î app_name °Ê³°¤Ë¡¢3¤Ä¤Îʸ»úÎó¤ò¥ê¥½¡¼¥¹¤È¤·¤ÆÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£

  • colors.xml
        1	<?xml version="1.0" encoding="utf-8"?>
        2	<!--
        3	 * Copyright (c) 2013, Ryo Ichinose
        4	 * All rights reserved.
        5	 *
        6	 * Redistribution and use in source and binary forms, with or without 
        7	 * modification, are permitted provided that the following conditions are met:
        8	 *
        9	 *    * Redistributions of source code must retain the above copyright 
       10	 *      notice, this list of conditions and the following disclaimer.
       11	 *    * Redistributions in binary form must reproduce the above copyright 
       12	 *      notice, this list of conditions and the following disclaimer in the 
       13	 *      documentation and/or other materials provided with the distribution.
       14	 *    * Neither the name of the auther nor the 
       15	 *      names of its contributors may be used to endorse or promote products 
       16	 *      derived from this software without specific prior written permission.
       17	 *
       18	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       19	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
       20	 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
       21	 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
       22	 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
       23	 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
       24	 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       25	 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
       26	 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
       27	 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
       28	 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       29	-->
       30	
       31	<resources>
       32	    <color name="color_bg">#000000</color>
       33	    <color name="color_fg">#00ffff</color>
       34	</resources>
    

    ¿§¥ê¥½¡¼¥¹ÄêµÁ¥Õ¥¡¥¤¥ë¤Ç¤¹¡£2¤Ä¤Î¿§¤ò¥ê¥½¡¼¥¹¤È¤·¤ÆÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£¤³¤³¤Ç»ÈÍѤ·¤Æ¤¤¤ë¿§»ØÄê¤Ï #RRGGBB ¤Ç¤¹¡£

°Ê¾å¤Ç²òÀâ¤Ï½ª¤ï¤ê¤Ç¤¹¡£¼¡²ó¤Ï¡¢¼«ºî¤·¤¿ View ¤Ë²èÁü¤òɽ¼¨¤¹¤ëÊýË¡¤Ë¤Ä¤¤¤Æ½ñ¤­¤¿¤¤¤È»×¤¤¤Þ¤¹¡£

¡û ¥¦¥£¥ó¥É¥¦¥¿¥¤¥È¥ë¤òɽ¼¨¤·¤Ê¤¤

¢¬¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Î DispImage.java ¤Ç¡¢android.view.Window ¤ò import ¤·¤¿¾å¤Ç onCreate ¤Ë¤Æ requestWindowFeature(Window.FEATURE_NO_TITLE) ¤ò¼Â¹Ô¤¹¤ë¤È¡¢¥¦¥£¥ó¥É¥¦¥¿¥¤¥È¥ë¤¬É½¼¨¤µ¤ì¤Ê¤¯¤Ê¤ê¤Þ¤¹¡£

¢¨Activity#setContentView() ¤ÎÁ°¤Ë¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£

public final boolean Activity#requestWindowFeature(int featureId);

QVGA(320x480:mdpi) ¤Î¥¨¥ß¥å¥ì¡¼¥¿¤Ç¤Î¼Â¹Ô·ë²Ì¤Ï¢­¤ÎÍͤˤʤê¤Þ¤¹¡£¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÈÀâÌÀ ¤Î²èÌ̤Ȥ褯Èæ¤Ù¤ë¤È¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾¤òɽ¼¨¤·¤¿¥¿¥¤¥È¥ëÉô¤¬Ìµ¤¯¤Ê¤Ã¤Æ¤¤¤ë¤³¤È¤¬Ê¬¤«¤ë¤È»×¤¤¤Þ¤¹¡£
¥¦¥£¥ó¥É¥¦¥¿¥¤¥È¥ëÈóɽ¼¨