IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 移动开发作业一 -> 正文阅读

[移动开发]移动开发作业一

学习目标:

请根据课程实操实现APP门户界面框架设计,至少包含4个tab页,能实现tab页之间的点击切换;

学习内容:

1.请根据课程实操实现APP门户界面框架设计,至少包含4个tab页,能实现tab页之间的点击切换;
2.使用布局(layouts)和分段(fragment),对控件进行点击监听;

实验代码:

package com.zhy.weixin6.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

public class ChangeColorIconWithTextView extends View
{

private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;
/**
 * 颜色
 */
private int mColor = 0xFF45C01A;
/**
 * 透明度 0.0-1.0
 */
private float mAlpha = 0f;
/**
 * 图标
 */
private Bitmap mIconBitmap;
/**
 * 限制绘制icon的范围
 */
private Rect mIconRect;
/**
 * icon底部文本
 */
private String mText = "微信";
private int mTextSize = (int) TypedValue.applyDimension(
		TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics());
private Paint mTextPaint;
private Rect mTextBound = new Rect();

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

/**
 * 初始化自定义属性值
 * 
 * @param context
 * @param attrs
 */
public ChangeColorIconWithTextView(Context context, AttributeSet attrs)
{
	super(context, attrs);

	// 获取设置的图标
	TypedArray a = context.obtainStyledAttributes(attrs,
			R.styleable.ChangeColorIconView);

	int n = a.getIndexCount();
	for (int i = 0; i < n; i++)
	{

		int attr = a.getIndex(i);
		switch (attr)
		{
		case R.styleable.ChangeColorIconView_icon:
			BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
			mIconBitmap = drawable.getBitmap();
			break;
		case R.styleable.ChangeColorIconView_color:
			mColor = a.getColor(attr, 0x45C01A);
			break;
		case R.styleable.ChangeColorIconView_text:
			mText = a.getString(attr);
			break;
		case R.styleable.ChangeColorIconView_text_size:
			mTextSize = (int) a.getDimension(attr, TypedValue
					.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10,
							getResources().getDisplayMetrics()));
			break;

		}
	}

	a.recycle();

	mTextPaint = new Paint();
	mTextPaint.setTextSize(mTextSize);
	mTextPaint.setColor(0xff555555);
	// 得到text绘制范围
	mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound);

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	// 得到绘制icon的宽
	int bitmapWidth = Math.min(getMeasuredWidth() - getPaddingLeft()
			- getPaddingRight(), getMeasuredHeight() - getPaddingTop()
			- getPaddingBottom() - mTextBound.height());

	int left = getMeasuredWidth() / 2 - bitmapWidth / 2;
	int top = (getMeasuredHeight() - mTextBound.height()) / 2 - bitmapWidth
			/ 2;
	// 设置icon的绘制范围
	mIconRect = new Rect(left, top, left + bitmapWidth, top + bitmapWidth);

}

@Override
protected void onDraw(Canvas canvas)
{

	int alpha = (int) Math.ceil((255 * mAlpha));
	canvas.drawBitmap(mIconBitmap, null, mIconRect, null);
	setupTargetBitmap(alpha);
	drawSourceText(canvas, alpha);
	drawTargetText(canvas, alpha);
	canvas.drawBitmap(mBitmap, 0, 0, null);

}

private void setupTargetBitmap(int alpha)
{
	mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
			Config.ARGB_8888);
	mCanvas = new Canvas(mBitmap);
	mPaint = new Paint();
	mPaint.setColor(mColor);
	mPaint.setAntiAlias(true);
	mPaint.setDither(true);
	mPaint.setAlpha(alpha);
	mCanvas.drawRect(mIconRect, mPaint);
	mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
	mPaint.setAlpha(255);
	mCanvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint);
}

private void drawSourceText(Canvas canvas, int alpha)
{
	mTextPaint.setTextSize(mTextSize);
	mTextPaint.setColor(0xff333333);
	mTextPaint.setAlpha(255 - alpha);
	canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2
			- mTextBound.width() / 2,
			mIconRect.bottom + mTextBound.height(), mTextPaint);
}

private void drawTargetText(Canvas canvas, int alpha)
{
	mTextPaint.setColor(mColor);
	mTextPaint.setAlpha(alpha);
	canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2
			- mTextBound.width() / 2,
			mIconRect.bottom + mTextBound.height(), mTextPaint);
	
}





public void setIconAlpha(float alpha)
{
	this.mAlpha = alpha;
	invalidateView();
}

private void invalidateView()
{
	if (Looper.getMainLooper() == Looper.myLooper())
	{
		invalidate();
	} else
	{
		postInvalidate();
	}
}

public void setIconColor(int color)
{
	mColor = color;
}

public void setIcon(int resId)
{
	this.mIconBitmap = BitmapFactory.decodeResource(getResources(), resId);
	if (mIconRect != null)
		invalidateView();
}

public void setIcon(Bitmap iconBitmap)
{
	this.mIconBitmap = iconBitmap;
	if (mIconRect != null)
		invalidateView();
}

private static final String INSTANCE_STATE = "instance_state";
private static final String STATE_ALPHA = "state_alpha";

@Override
protected Parcelable onSaveInstanceState()
{
	Bundle bundle = new Bundle();
	bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
	bundle.putFloat(STATE_ALPHA, mAlpha);
	return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable state)
{
	if (state instanceof Bundle)
	{
		Bundle bundle = (Bundle) state;
		mAlpha = bundle.getFloat(STATE_ALPHA);
		super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
	} else
	{
		super.onRestoreInstanceState(state);
	}

}

}

package com.zhy.weixin6.ui;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.Window;

@SuppressLint(“NewApi”)
public class MainActivity extends FragmentActivity implements
OnPageChangeListener, OnClickListener
{
private ViewPager mViewPager;
private List mTabs = new ArrayList();
private FragmentPagerAdapter mAdapter;

private String[] mTitles = new String[] { "First Fragment!",
		"Second Fragment!", "Third Fragment!", "Fourth Fragment!" };

private List<ChangeColorIconWithTextView> mTabIndicator = new ArrayList<ChangeColorIconWithTextView>();

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

	setOverflowShowingAlways();
	getActionBar().setDisplayShowHomeEnabled(false);
	mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

	initDatas();

	mViewPager.setAdapter(mAdapter);
	mViewPager.setOnPageChangeListener(this);
}

private void initDatas()
{

	for (String title : mTitles)
	{
		TabFragment tabFragment = new TabFragment();
		Bundle args = new Bundle();
		args.putString("title", title);
		tabFragment.setArguments(args);
		mTabs.add(tabFragment);
	}

	mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
	{

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

		@Override
		public Fragment getItem(int arg0)
		{
			return mTabs.get(arg0);
		}
	};

	initTabIndicator();

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
}

private void initTabIndicator()
{
	ChangeColorIconWithTextView one = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_one);
	ChangeColorIconWithTextView two = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_two);
	ChangeColorIconWithTextView three = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_three);
	ChangeColorIconWithTextView four = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_four);

	mTabIndicator.add(one);
	mTabIndicator.add(two);
	mTabIndicator.add(three);
	mTabIndicator.add(four);

	one.setOnClickListener(this);
	two.setOnClickListener(this);
	three.setOnClickListener(this);
	four.setOnClickListener(this);

	one.setIconAlpha(1.0f);
}

@Override
public void onPageSelected(int arg0)
{
}

@Override
public void onPageScrolled(int position, float positionOffset,
		int positionOffsetPixels)
{
	// Log.e("TAG", "position = " + position + " , positionOffset = "
	// + positionOffset);

	if (positionOffset > 0)
	{
		ChangeColorIconWithTextView left = mTabIndicator.get(position);
		ChangeColorIconWithTextView right = mTabIndicator.get(position + 1);

		left.setIconAlpha(1 - positionOffset);
		right.setIconAlpha(positionOffset);
	}

}

@Override
public void onPageScrollStateChanged(int state)
{

}

@Override
public void onClick(View v)
{

	resetOtherTabs();

	switch (v.getId())
	{
	case R.id.id_indicator_one:
		mTabIndicator.get(0).setIconAlpha(1.0f);
		mViewPager.setCurrentItem(0, false);
		break;
	case R.id.id_indicator_two:
		mTabIndicator.get(1).setIconAlpha(1.0f);
		mViewPager.setCurrentItem(1, false);
		break;
	case R.id.id_indicator_three:
		mTabIndicator.get(2).setIconAlpha(1.0f);
		mViewPager.setCurrentItem(2, false);
		break;
	case R.id.id_indicator_four:
		mTabIndicator.get(3).setIconAlpha(1.0f);
		mViewPager.setCurrentItem(3, false);
		break;

	}

}

/**
 * 重置其他的Tab
 */
private void resetOtherTabs()
{
	for (int i = 0; i < mTabIndicator.size(); i++)
	{
		mTabIndicator.get(i).setIconAlpha(0);
	}
}

@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
	if (featureId == Window.FEATURE_ACTION_BAR && menu != null)
	{
		if (menu.getClass().getSimpleName().equals("MenuBuilder"))
		{
			try
			{
				Method m = menu.getClass().getDeclaredMethod(
						"setOptionalIconsVisible", Boolean.TYPE);
				m.setAccessible(true);
				m.invoke(menu, true);
			} catch (Exception e)
			{
			}
		}
	}
	return super.onMenuOpened(featureId, menu);
}

private void setOverflowShowingAlways()
{
	try
	{
		// true if a permanent menu key is present, false otherwise.
		ViewConfiguration config = ViewConfiguration.get(this);
		Field menuKeyField = ViewConfiguration.class
				.getDeclaredField("sHasPermanentMenuKey");
		menuKeyField.setAccessible(true);
		menuKeyField.setBoolean(config, false);
	} catch (Exception e)
	{
		e.printStackTrace();
	}
}

}

package com.zhy.weixin6.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TabFragment extends Fragment
{
private String mTitle = “Default”;

public TabFragment()
{
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState)
{
	if (getArguments() != null)
	{
		mTitle = getArguments().getString("title");
	}

	TextView textView = new TextView(getActivity());
	textView.setTextSize(20);
	textView.setBackgroundColor(Color.parseColor("#ffffffff"));
	textView.setGravity(Gravity.CENTER);
	textView.setText(mTitle);
	return textView;
}

}

实验截图:

在这里插入图片描述
在这里插入图片描述

实验心得:

这一次的实验初次了解到了安卓开发技术,了解了as的相关功能的使用,学习到了相关的实验操作。再次接触到前端的开发,其中一些控件的应用需要熟练的运用起来,然后掌握一些java的语法的基础才能完成好实验的进行。

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-10-09 16:23:57  更:2021-10-09 16:24:14 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 0:30:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码