Skip to content

Commit

Permalink
finish PracticeDraw3
Browse files Browse the repository at this point in the history
  • Loading branch information
lengzehua committed May 21, 2018
1 parent 2c2d9ee commit 30e3502
Show file tree
Hide file tree
Showing 94 changed files with 2,081 additions and 4 deletions.
30 changes: 30 additions & 0 deletions PracticeDraw3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
![](images/icon.png)

HenCoder 绘制 3 练习项目
===

### 这是什么?

这不是一个独立使用的项目,它是 [HenCoder Android 开发进阶:UI 1-3 文字的绘制](http://hencoder.com/ui-1-3) 的配套练习项目。

### 怎么用?

项目是一个可以直接运行的 Android App 项目,项目运行后,在手机上打开是这样的:

![](images/preview.png)

工程下有一个 `/practice` 目录:

![](images/project_practice.png)

你要做的是就是,在 `/practice` 下的每一个 `PracticeXxxView.java` 文件中写代码,绘制出和页面上半部分相同的效果。例如写 `PracticeSetUnderlineTextView.java` 以绘制出带有下划线的文字。就像这样:

![](images/preview_after.png)

> 当然,没必要做得和示例一毛一样。这是一个练习,而不是一个超级模仿秀,关键是把技能掌握。
如果做不出来,可以参考 `/sample` 目录下的代码:

![](images/project_sample.png)

练习做完,绘制第三期(文字的绘制)的内容也就掌握得差不多了。
1 change: 1 addition & 0 deletions PracticeDraw3/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions PracticeDraw3/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.hencoder.hencoderpracticedraw3"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-core-ui:25.3.1'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
}
25 changes: 25 additions & 0 deletions PracticeDraw3/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/rengwuxian/.android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.hencoder.hencoderpracticedraw3;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.hencoder.hencoderpracticedraw3", appContext.getPackageName());
}
}
20 changes: 20 additions & 0 deletions PracticeDraw3/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hencoder.hencoderpracticedraw3">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.hencoder.hencoderpracticedraw3.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.hencoder.hencoderpracticedraw3;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager pager;
List<PageModel> pageModels = new ArrayList<>();

{
pageModels.add(new PageModel(R.layout.sample_draw_text, R.string.title_draw_text, R.layout.practice_draw_text));
pageModels.add(new PageModel(R.layout.sample_static_layout, R.string.title_static_layout, R.layout.practice_static_layout));
pageModels.add(new PageModel(R.layout.sample_set_text_size, R.string.title_set_text_size, R.layout.practice_set_text_size));
pageModels.add(new PageModel(R.layout.sample_set_typeface, R.string.title_set_typeface, R.layout.practice_set_typeface));
pageModels.add(new PageModel(R.layout.sample_set_fake_bold_text, R.string.title_set_fake_bold_text, R.layout.practice_set_fake_bold_text));
pageModels.add(new PageModel(R.layout.sample_set_strike_thru_text, R.string.title_set_strike_thru_text, R.layout.practice_set_strike_thru_text));
pageModels.add(new PageModel(R.layout.sample_set_underline_text, R.string.title_set_underline_text, R.layout.practice_set_underline_text));
pageModels.add(new PageModel(R.layout.sample_set_text_skew_x, R.string.title_set_text_skew_x, R.layout.practice_set_text_skew_x));
pageModels.add(new PageModel(R.layout.sample_set_text_scale_x, R.string.title_set_text_scale_x, R.layout.practice_set_text_scale_x));
pageModels.add(new PageModel(R.layout.sample_set_text_align, R.string.title_set_text_align, R.layout.practice_set_text_align));
pageModels.add(new PageModel(R.layout.sample_get_font_spacing, R.string.title_get_font_spacing, R.layout.practice_get_font_spacing));
pageModels.add(new PageModel(R.layout.sample_measure_text, R.string.title_measure_text, R.layout.practice_measure_text));
pageModels.add(new PageModel(R.layout.sample_get_text_bounds, R.string.title_get_text_bounds, R.layout.practice_get_text_bounds));
pageModels.add(new PageModel(R.layout.sample_get_font_metrics, R.string.title_get_font_metrics, R.layout.practice_get_font_metrics));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

@Override
public Fragment getItem(int position) {
PageModel pageModel = pageModels.get(position);
return PageFragment.newInstance(pageModel.sampleLayoutRes, pageModel.practiceLayoutRes);
}

@Override
public int getCount() {
return pageModels.size();
}

@Override
public CharSequence getPageTitle(int position) {
return getString(pageModels.get(position).titleRes);
}
});

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(pager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}

private class PageModel {
@LayoutRes int sampleLayoutRes;
@StringRes int titleRes;
@LayoutRes int practiceLayoutRes;

PageModel(@LayoutRes int sampleLayoutRes, @StringRes int titleRes, @LayoutRes int practiceLayoutRes) {
this.sampleLayoutRes = sampleLayoutRes;
this.titleRes = titleRes;
this.practiceLayoutRes = practiceLayoutRes;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.hencoder.hencoderpracticedraw3;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

public class PageFragment extends Fragment {
@LayoutRes int sampleLayoutRes;
@LayoutRes int practiceLayoutRes;

public static PageFragment newInstance(@LayoutRes int sampleLayoutRes, @LayoutRes int practiceLayoutRes) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putInt("sampleLayoutRes", sampleLayoutRes);
args.putInt("practiceLayoutRes", practiceLayoutRes);
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);

ViewStub sampleStub = (ViewStub) view.findViewById(R.id.sampleStub);
sampleStub.setLayoutResource(sampleLayoutRes);
sampleStub.inflate();

ViewStub practiceStub = (ViewStub) view.findViewById(R.id.practiceStub);
practiceStub.setLayoutResource(practiceLayoutRes);
practiceStub.inflate();

return view;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
sampleLayoutRes = args.getInt("sampleLayoutRes");
practiceLayoutRes = args.getInt("practiceLayoutRes");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.hencoder.hencoderpracticedraw3.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class Practice01DrawTextView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
String text = "Hello HenCoder";

public Practice01DrawTextView(Context context) {
super(context);
}

public Practice01DrawTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public Practice01DrawTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

{
paint.setTextSize(60);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

// 使用 drawText() 来绘制文字
// 文字坐标: (50, 100)
canvas.drawText(text, 50, 100, paint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.hencoder.hencoderpracticedraw3.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

public class Practice02StaticLayoutView extends View {
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
String text = "Hello\nHenCoder";
StaticLayout staticLayout;

public Practice02StaticLayoutView(Context context) {
super(context);
}

public Practice02StaticLayoutView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public Practice02StaticLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

{
textPaint.setTextSize(60);
staticLayout = new StaticLayout(text, textPaint, 600, Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

// 使用 StaticLayout 代替 Canvas.drawText() 来绘制文字,
// 以绘制出带有换行的文字
staticLayout.draw(canvas);
}
}
Loading

0 comments on commit 30e3502

Please sign in to comment.