• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.google.wearable.app;
18 
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.app.FragmentManager;
22 import android.graphics.Bitmap;
23 import android.graphics.Canvas;
24 import android.graphics.Color;
25 import android.graphics.Paint;
26 import android.graphics.Point;
27 import android.graphics.Typeface;
28 import android.os.Bundle;
29 import android.support.wearable.view.CardFragment;
30 import android.support.wearable.view.FragmentGridPagerAdapter;
31 import android.support.wearable.view.GridViewPager;
32 import android.support.wearable.view.ImageReference;
33 import java.util.HashMap;
34 import java.util.Map;
35 
36 public class GridExampleActivity extends Activity {
37     private static final int NUM_ROWS = 10;
38     private static final int NUM_COLS = 3;
39 
40     MainAdapter mAdapter;
41     GridViewPager mPager;
42 
43 
44     @Override
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         setContentView(R.layout.grid_activity);
48         mPager = (GridViewPager) findViewById(R.id.fragment_container);
49         mAdapter = new MainAdapter(getFragmentManager());
50         mPager.setAdapter(mAdapter);
51 
52     }
53 
54     private static class MainAdapter extends FragmentGridPagerAdapter{
55         Map<Point, ImageReference> mBackgrounds = new HashMap<Point, ImageReference>();
56 
MainAdapter(FragmentManager fm)57         public MainAdapter(FragmentManager fm) {
58             super(fm);
59         }
60 
61         @Override
getRowCount()62         public int getRowCount() {
63             return NUM_ROWS;
64         }
65 
66         @Override
getColumnCount(int rowNum)67         public int getColumnCount(int rowNum) {
68             return NUM_COLS;
69         }
70 
71         @Override
getFragment(int rowNum, int colNum)72         public Fragment getFragment(int rowNum, int colNum) {
73             return MainFragment.newInstance(rowNum, colNum);
74         }
75 
76         @Override
getBackground(int row, int column)77         public ImageReference getBackground(int row, int column) {
78             Point pt = new Point(column, row);
79             ImageReference ref = mBackgrounds.get(pt);
80             if (ref == null) {
81                 Bitmap bm = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
82                 Canvas c = new Canvas(bm);
83                 Paint p = new Paint();
84                 // Clear previous image.
85                 c.drawRect(0, 0, 200, 200, p);
86                 p.setAntiAlias(true);
87                 p.setTypeface(Typeface.DEFAULT);
88                 p.setTextSize(64);
89                 p.setColor(Color.LTGRAY);
90                 c.drawText(column+ "-" + row, 20, 100, p);
91                 ref = ImageReference.forBitmap(bm);
92                 mBackgrounds.put(pt, ref);
93             }
94             return ref;
95         }
96     }
97 
98     public static class MainFragment extends CardFragment {
newInstance(int rowNum, int colNum)99         private static MainFragment newInstance(int rowNum, int colNum) {
100             Bundle args = new Bundle();
101             args.putString(CardFragment.KEY_TITLE, "Row:" + rowNum);
102             args.putString(CardFragment.KEY_TEXT, "Col:" + colNum);
103             MainFragment f = new MainFragment();
104             f.setArguments(args);
105             return f;
106         }
107     }
108 }
109