分享

How to add a scrolling canvas using scrollview | Software Health Club

 quasiceo 2017-02-08

How to add a scrolling canvas using scrollview

To allow a canvas to scroll horizontally and vertically using scrollviews:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//////////////////////////////////////////////////////////////////////////////////////
public class Main extends Activity
{
// public this_canvas this_canvas;
public DrawView draw_view;
public ScrollView scroll_view;
public HorizontalScrollView h_scroll_view;
public LinearLayout lin_layout;
public Canvas this_canvas;
@Override
public void onCreate(Bundle savedInstanceState)
{
DrawView main_draw_view;
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
lin_layout = new LinearLayout(this);
scroll_view = new ScrollView(this);
h_scroll_view = new HorizontalScrollView(this);
scroll_view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
h_scroll_view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
lin_layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
lin_layout.setOrientation(LinearLayout.VERTICAL);
Bitmap mBitmap = Bitmap.createBitmap(630, 870, Bitmap.Config.ARGB_8888);
this_canvas = new Canvas(mBitmap);
main_draw_view = new DrawView(this);
main_draw_view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
scroll_view.addView(main_draw_view);
h_scroll_view.addView(scroll_view);
setContentView(h_scroll_view);
}
//////////////////////////////////////////////////////////////////////////////////////
public class DrawView extends View
{
Context app_context;
public DrawView(Context context)
{
super(context);
app_context = context;
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
// onMeasure must be included otherwise one or both scroll views will be compressed to zero pixels
// and the scrollview will then be invisible
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int width = 2000;
int height = 3000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
setMeasuredDimension(width, height);
}
@Override
public void onDraw(Canvas canvas)
{
this_canvas = canvas;
// ToDo: Put drawing code in here
}
}
}

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多