Long Luo's Life Notes

每一天都是奇迹

By Long Luo

在上一篇 Android自定义View:如何实现一个模拟时钟? 中我们实现了一款模拟时钟。在上一篇中,我们使用了Canvas中的canvas.rotate()方法让坐标系旋转从而绘制出指针。

但是,其实我们也可以使用另外一种方法去实现指针绘制,这种更直观,更容易理解。

一. 绘制指针

要绘制指针,需要知道指针的起点和终点,而起点就是圆心,终点就需要根据指针的sincos值及圆心进行对应的计算。

弧度

要计算指针对应的 sincos 值,区别于上一篇的角度,这一篇我们使用弧度。具体弧度是什么以及角度弧度换算关系可自行Google。

下面我们给出对应指针弧度如何计算的,如下所示:

1
2
3
float secRot = mCalendar.get(Calendar.SECOND) / 30f * (float) Math.PI;
float minRot = mCalendar.get(Calendar.MINUTE) / 30f * (float) Math.PI;
float hrRot = (((mCalendar.get(Calendar.HOUR_OF_DAY) + (mCalendar.get(Calendar.MINUTE) / 60f))) / 6f) * (float) Math.PI;

绘制

知道对应指针的弧度之后,我们就很容易计算出对应的指针坐标了,那么绘制也变得简单了,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float secLength = centerX - 60;
float minLength = centerX - 80;
float hrLength = centerX - 100;

float hrX = (float) Math.sin(hrRot) * hrLength;
float hrY = (float) -Math.cos(hrRot) * hrLength;
canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, paintHour);

float minX = (float) Math.sin(minRot) * minLength;
float minY = (float) -Math.cos(minRot) * minLength;
canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, paintMinute);

float secX = (float) Math.sin(secRot) * secLength;
float secY = (float) -Math.cos(secRot) * secLength;
canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, paintSecond);

小插曲

开发完成之后,发现了一个Bug,指针一直不动,打印Log发现对应的弧度一直不变,那么很明显就是时间没有刷新,在计算弧度前,设置为当前时间即可。

1
mCalendar.setTimeInMillis(System.currentTimeMillis());
阅读全文 »

By Long Luo

一个类的定义放在另一个类的定义内部,这就是内部类

先来段代码,对内部类有个直观认识:

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
public class OuterClass {
private String name ;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

class InnerClass{
public InnerClass(){
name = "chenssy";
age = 23;
}
}
}

在这里InnerClass就是内部类。

Java中有4种不同类型的Java内部类,下面我们将一一用实例来介绍:

1. 静态内部类(static nested classes)

关键字static可以修饰成员变量、方法、代码块,还可以修饰内部类,而使用static修饰的内部类我们称之为静态内部类,不过我们更喜欢称之为嵌套内部类。

静态内部类与非静态内部类之间存在一个最大的区别,我们知道非静态内部类在编译完成之后会隐含地保存着一个引用,该引用是指向创建它的外围内,但是静态内部类却没有。没有这个引用就意味着:

  1. 它的创建是不需要依赖于外围类的。
  2. 它不能使用任何外围类的非static成员变量和方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Outer {
static class Inner {
void go() {
System.out.println(" Inner class reference is: " + this);
}
}
}

public class Test {
public static void main(String[] args) {
Outer.Inner n = new Outer.Inner();
n.go();
}
}

Output:

1
Inner class reference is: com.longluo.java.interview.innerclass.Outer$Inner@15db9742
阅读全文 »

HashMap is very useful when a counter is required.

1
2
3
4
5
6
7
8
HashMap<String, Integer> countMap = new HashMap<String , Integer>();

// ... a lot of a’s like the following
if (countMap.keySet().contains(a)) {
countMap.put(a, countMap.get(a) + 1);
} else {
countMap.put(a, 1);
}

loop through hashmap

1
2
3
4
5
6
7
8
9
10
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + " , Value = " + entry.getValue());
}

2 print hashmap

1
2
3
4
5
6
7
8
9
10
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();

while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();

System.out.println(pairs.getKey() + " = " + pairs.getValue());
it.remove(); // avoids a Concurrent Modification Exception
}
}
阅读全文 »

1. what is inner interface in java?

Inner interface is also called nested interface, which means declare an interface inside of another interface. For example, the Entry interface is declared in the Map interface.

1
2
3
4
5
6
7
public interface Map {
interface Entry {
int getKey();
}
}

void clear();

2. why use inner interface?

There are several compelling reasons for using inner interface: 1. It is a way of logically grouping interfaces that are only used in one place. 2. It increases encapsulation. 3. Nested interfaces can lead to more readable and maintainable code.

One example of inner interface used in java standard library is java.util.Map and Java.util.Map.Entry. Here java.util.Map is used also as a namespace. Entry does not belong to the global scope, which means there are many other entities that are 64

3. HOW INNER INTERFACE WORKS?

Entries and are not necessary Map’s entries. This indicates that Entry represents entries related to the Map.

18.3 how inner interface works? To figure out how inner interface works, we can compare it with nested classes. Nested classes can be considered as a regular method declared in outer class. Since a method can be declared as static or non-static, similarly nested classes can be static and non-static. Static class is like a static method, it can only access outer class members through objects. Non-static class can access any member of the outer class.

18.4. A SIMPLE EXAMPLE OF INNER INTERFACE?

Because an interface can not be instantiated, the inner interface only makes sense if it is static. Therefore, by default inter interface is static, no matter you manually add static or not. 18.4 a simple example of inner interface?

Map.java public int e r f a c e Map {

18.4. A SIMPLE EXAMPLE OF INNER INTERFACE?

int e r f a c e Entry { int getKey ( ) ; } void c l e a r ( ) ; } MapImpl.java public c l a s s MapImpl implements Map { c l a s s ImplEntry implements Map. Entry { public int getKey ( ) { return 0 ; } } @Override public void c l e a r ( ) { / / c l e a r } }

翻译 By Long Luo

本文翻译自 The Interface and Class Hierarchy Diagram of Java Collections ,主要通过一系列简单易懂的图片让你迅速了解Java容器类,容器接口以及类层级关系。

大段文字会看得很烦,图片才是王道!

一、 Collection vs Collections

“Collection”和”Collections”是2个完全不同的概念,在Java容器的类层级图中,“Collection”是一个根接口,但是”Collections”仅仅只是一个提供多种静态方法的类用于操作一些Collection类型。

Collection Vs Collections

二、 Collection的类层级图

下图展示了Collection的类层级图:

java-collection-hierarchy

三、 Map的类层级图

下图是一张Map的类层级图:

MapClassHierarchy

四、 总结

collection summary

五、 代码示例

下面展示容器类的一个代码示例:

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
List<String> a1 = new ArrayList<String>();
a1.add("Program");
a1.add("Creek");
a1.add("Java");
a1.add("Java");
System.out.println("ArrayList Elements");
System.out.print("\t" + a1 + "\n");

List<String> l1 = new LinkedList<String>();
l1.add("Program");
l1.add("Creek");
l1.add("Java");
l1.add("Java");
System.out.println("LinkedList Elements");
System.out.print("\t" + l1 + "\n");

Set<String> s1 = new HashSet<String>(); // or new TreeSet() will order the elements;
s1.add("Program");
s1.add("Creek");
s1.add("Java");
s1.add("Java");
s1.add("tutorial");
System.out.println("Set Elements");
System.out.print("\t" + s1 + "\n");

Map<String, String> m1 = new HashMap<String, String>(); // or new TreeMap() will order based on keys
m1.put("Windows", "2000");
m1.put("Windows", "XP");
m1.put("Language", "Java");
m1.put("Website", "programcreek.com");
System.out.println("Map Elements");
System.out.print("\t" + m1);

输出如下:

1
2
3
4
5
6
7
8
ArrayList Elements
[Program, Creek, Java, Java]
LinkedList Elements
[Program, Creek, Java, Java]
Set Elements
[tutorial, Creek, Program, Java]
Map Elements
{Windows=XP, Website=programcreek.com, Language=Java}

以上!

0%