转载请注明出处:王亟亟的大牛之路
git上看到一个不错的“自定义寄生View”,个人觉得还是很有拓展性的,写篇博文介绍下
目录结构+运行效果
AS的项目,内容不多,就2个类,想整合进自己项目的话也不难
MainActivity
public class MainActivity extends ActionBarActivity
implements OverMenuView.OnSelectionChangeListener, OverMenuView.OnMenuVisibilityChangeListener {
private static final String TAG = "MainActivity";
private ImageView mImageView;
private OverMenuView overMenuView;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image);
textView = (TextView) findViewById(R.id.text);
overMenuView = (OverMenuView) findViewById(R.id.overmenu);
overMenuView.setOnSelectionChangedListener(this);
overMenuView.setOnMenuVisibilityChangeListener(this);
try {
loadImage("image.jpg");
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadImage(@NonNull final String file) throws IOException {
final InputStream input = getAssets().open(file);
Bitmap bitmap = BitmapFactory.decodeStream(input);
mImageView.setImageBitmap(bitmap);
input.close();
}
@Override
public void onSelectionChanged(final int position) {
Log.d(TAG, "onSelectionChanged: " + position);
textView.setText(overMenuView.getEntries()[position]);
}
@Override
public void onVisibilityChanged(final View view, final boolean visible) {
Log.d(TAG, "onVisibilityChanged: " + view + ", " + visible);
}
}
分析:
没有参杂什么逻辑,只是把assets目录下的图片set到imageview中并且把选择的结果放入textView呈现而已。
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image" />
<it.sephiroth.android.library.overlaymenu.OverMenuView
app:overmenu_centerInView="@+id/image"
android:entries="@array/overmenuEntries"
android:id="@+id/overmenu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout
android:layout_marginTop="@dimen/activity_vertical_margin"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:gravity="center"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="90dip">
<TextView
android:text="@string/waiting_for_selection"
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
分析:
这里有一个自定义的标签xmlns:app="http://schemas.android.com/apk/res-auto"
也就是将我们的时间“绑定到”ImageView上去了而已!
下拉列表的数据源:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="overmenuEntries">
<item>First Item</item>
<item>Second Item</item>
<item>Third Item</item>
<item>Fourth Item</item>
<item>Fifth Item</item>
<item>Sixth Item</item>
<item>Seventh Item</item>
</string-array>
</resources>
具体代码可以看源码,源码地址:http://yunpan.cn/cmxaWSzZH3mqQ 访问密码 f898
时间: 2024-11-01 13:00:58