Skip to content

Commit

Permalink
支持图片交叠
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Aug 22, 2015
1 parent dcd46f8 commit 8e4bf24
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
45 changes: 43 additions & 2 deletions mixturetextview/src/main/java/com/zhy/view/MixtureTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -226,7 +227,6 @@ private void getAllYCors()
corYSet.clear();
mViewBounds.clear();


//获得所有的y轴坐标
int cCount = getChildCount();
for (int i = 0; i < cCount; i++)
Expand Down Expand Up @@ -316,7 +316,9 @@ private void initAllNeedRenderRect()
{
Rect ra = rs.get(j);
Rect rb = rs.get(j + 1);
viewRectBetween2Y.add(new Rect(ra.right, y1, rb.left, y2));

if (ra.right < rb.left)
viewRectBetween2Y.add(new Rect(ra.right, y1, rb.left, y2));
}
//add last
Rect lastRect = rs.get(rs.size() - 1);
Expand Down Expand Up @@ -452,6 +454,45 @@ private List<Rect> caculateViewYBetween(int y1, int y2)
rs.add(tmp);
}
}


//TODO ADD
Collections.sort(rs, new Comparator<Rect>()
{
@Override
public int compare(Rect lhs, Rect rhs)
{
if (lhs.left > rhs.left) return 1;
return -1;
}
});


if (rs.size() >= 2)
{
List<Rect> res = new ArrayList<Rect>(rs);
Rect pre = rs.get(0), next = rs.get(1);
//合并
for (int i = 1; i < rs.size(); i++)
{
//if相交
if (Rect.intersects(pre, next))
{
int left = Math.min(pre.left, next.left);
int right = Math.max(pre.right, next.right);

res.remove(pre);
res.remove(next);
res.add(pre = new Rect(left, y1, right, y2));
} else
{
pre = next;
}
next = rs.get(i);
}

rs = res;
}
return rs;
}

Expand Down
18 changes: 11 additions & 7 deletions mixturetextview/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,32 @@
android:padding="16dp"
android:layout_height="wrap_content"
android:background="#f4f3ff"
android:text="@string/text2"
android:text="@string/text1"
android:textColor="#ff5d75"
android:textSize="14sp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:layout_alignParentRight="true"
android:src="@drawable/icon"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>

android:src="@drawable/android2" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/android2"/>
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:src="@drawable/icon"/>






</com.zhy.view.MixtureTextView>

Expand Down
13 changes: 8 additions & 5 deletions mixturetextview/src/main/res/layout/activity_main02.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@
android:padding="20dp"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android2" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"

android:layout_marginTop="100dp"
android:src="@drawable/icon"/>

<ImageView
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="@drawable/icon"/>


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/android2"/>


</RelativeLayout>

Expand Down

0 comments on commit 8e4bf24

Please sign in to comment.