这是去年大二做的一个简单音乐播放器项目:是尽可能模仿酷狗音乐写的,具体实现的功能如下:
1:启动动画:点击运行程序会出现一个两秒钟的视频,类似酷狗音乐的启动动画一样,非常可观!
2:登录注册界面:输入账号和密码检验信息登录!
3:轮播图:和酷狗音乐的一模一样,在主界面的上方有一个自动循环的轮播图,点击轮播图的每一个图片信息即可进入对应的具体服务,非常具有加分点!
4:音乐唱片的转盘,歌曲同步进度条,以及音乐的暂停/播放/继续/上下曲切换!
5:音乐的搜索实现!
6:视频专栏的播放!
7;个人信息界面的布局实现,如反馈,评分,更多,性别年龄昵称,收藏等!
轮播图代码如下:
package com.ypc.xiaoxiongmusic;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static android.view.View.VISIBLE;
@SuppressLint("ValidFragment")
public class musicFragment extends Fragment {
ListView lv;
SearchView searchView;
private ViewPager mViewPaper;
private List<ImageView> images;
private List<View> dots;
private int currentItem;
//记录上一次点的位置
private int oldPosition = 0;
//存放图片的id
private int selectedPosition=0;
private ImageView bf;
private int[] imageIds = new int[]{
R.drawable.lunchatu1,
R.drawable.lunchatu2,
R.drawable.lunchatu3,
R.drawable.lunchatu2,
R.drawable.lunchatu3
};
//存放图片的标题
private String[] titles = new String[]{
"再次相见,还是你",
"这一天,我等了好久",
"天才少女再次回归",
"畅想音乐,点亮你我",
"HELLO,DY.MEMORY"
};
private TextView title;
private ViewPagerAdapter adapter;
private ScheduledExecutorService scheduledExecutorService;
private static List<Integer> sPics=new ArrayList<>();
//当时测试颜色用的
/* static {
sPics.add(R.drawable.b1);
sPics.add(R.drawable.b2);
sPics.add(R.drawable.b3);
}*/
private ImageView m1,m2,m3,m4;
private View view;
public static String[] name={"太妍 (??) - ????(听得见吗)","太妍 (??) - ???? ?? ?(济州岛的蓝夜)","太妍 (??) - ??? ??(还有一个)","太妍 (??) - Rescue Me","太妍 (??) - My Love (Duet Ver.)","华晨宇 - 好想爱这个世界啊(Live)",
"华晨宇 - 与火星的孩子对话","华晨宇 - 国王与乞丐","华晨宇 - 斗牛(Live)","华晨宇 - 烟火里的尘埃","华晨宇 - 疯人院(Live)","华晨宇 - 环游","华晨宇 - 寻","华晨宇 - 无聊人","华晨宇 - For Forever","华晨宇 - 蜉蝣","华晨宇 - 我们(Live)","华晨宇 - 异类",
"华晨宇 - 我管你","华晨宇 - 你要相信这不是最后一天","华晨宇 - 寒鸦少年","华晨宇 - 齐天","张艺兴 - 会好的","张艺兴 - 晚安","张艺兴 - 一个人","张艺兴 - 面罩 (Live)","张艺兴 - 外婆","张艺兴 - 我不好","张艺兴 - 祈愿","太妍 (??) - Gee (Busking Ver.)(Live)","太妍 (??) - ???(如果) (Busking Ver.)(Live)",
"太妍 (??) - bad guy (Busking Ver.)(Live)","太妍 (??) - ??? ?? ??(聚集记忆的时间) (Busking Ver.)(Live)","张国荣 - 怪你过份美丽","张国荣 - 左右手","张国荣 - 至少还有你 (00 Live)(live版)","张国荣 - 我"};
public static int[] icons={R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx
,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx
,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx,R.drawable.musicx};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_music, container, false);
//lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, name));
searchView =view.findViewById(R.id.sv);
lv=view.findViewById(R.id.lv);
lv.setTextFilterEnabled(true);
// 设置搜索文本监听
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// 当点击搜索按钮时触发该方法
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
// 当搜索内容改变时触发该方法
@Override
public boolean onQueryTextChange(String newText) {
if (!TextUtils.isEmpty(newText)){
lv.setFilterText(newText);
}else{
lv.clearTextFilter();
}
return false;
}
});
bf=view.findViewById(R.id.bf);
mViewPaper = (ViewPager)view. findViewById(R.id.vp);
images = new ArrayList<ImageView>();
for(int i = 0; i < imageIds.length; i++){
ImageView imageView = new ImageView(getActivity());
imageView.setBackgroundResource(imageIds[i]);
images.add(imageView);
}
dots = new ArrayList<View>();
dots.add(view.findViewById(R.id.dot_0));
dots.add(view.findViewById(R.id.dot_1));
dots.add(view.findViewById(R.id.dot_2));
dots.add(view.findViewById(R.id.dot_3));
dots.add(view.findViewById(R.id.dot_4));
title = (TextView) view.findViewById(R.id.title);
title.setText(titles[0]);
adapter = new ViewPagerAdapter();
mViewPaper.setAdapter(adapter);
//论插图的ontauch点击方法
mViewPaper.setOnTouchListener(new View.OnTouchListener() {
int flage = 0 ;
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
flage = 0 ;
break ;
case MotionEvent.ACTION_MOVE:
flage = 1 ;
break ;
case MotionEvent.ACTION_UP :
if (flage == 0) {
int item = mViewPaper.getCurrentItem();
if (item == 0) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.kuaishou.com/f/X-5hKhok9ebbLDq1");//此处填链接
intent.setData(content_url);
startActivity(intent);
} else if (item == 1) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.kuaishou.com/f/X-5hKhok9ebbLDq1");//此处填链接
intent.setData(content_url);
startActivity(intent);
} else if (item == 2) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.kuaishou.com/f/X-5hKhok9ebbLDq1");//此处填链接
intent.setData(content_url);
startActivity(intent);
}else if (item == 3) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.kuaishou.com/f/X-5hKhok9ebbLDq1");//此处填链接
intent.setData(content_url);
startActivity(intent);
} else if (item == 4) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("https://www.kuaishou.com/f/X-5hKhok9ebbLDq1");//此处填链接
intent.setData(content_url);
startActivity(intent);
}
}
break ;
}
return false;
}
});
mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
title.setText(titles[position]);
dots.get(position).setBackgroundResource(R.drawable.red);
//Toast.makeText(getActivity(),"hurhurhghrui",Toast.LENGTH_SHORT).show();
dots.get(oldPosition).setBackgroundResource(R.drawable.pure);
oldPosition = position;
currentItem = position;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
m1=view.findViewById(R.id.m1);
m2=view.findViewById(R.id.m2);
m3=view.findViewById(R.id.m3);
m4=view.findViewById(R.id.m4);
m1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent4=new Intent(getActivity(),tukuActivity.class);
startActivity(intent4);
}
});
m2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent5=new Intent(getActivity(),paihangbangActivity.class);
startActivity(intent5);
}
});
m3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent6=new Intent(getActivity(),fenleiActivity.class);
startActivity(intent6);
}
});
//ListView listView=view.findViewById(R.id.lv);
MyBaseAdapter adapter=new MyBaseAdapter();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(getActivity(),Music_Activity.class);//创建Intent对象,启动check
//将数据存入Intent对象
intent.putExtra("name",name[position]);
intent.putExtra("position",String.valueOf(position));
startActivity(intent);
lv.setSelector(R.drawable.bf1);
}
});
return view;
}
private class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
// TODO Auto-generated method stub
view.removeView(images.get(position));
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
// TODO Auto-generated method stub
view.addView(images.get(position));
return images.get(position);
}
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}*/
/**
* 利用线程池定时执行动画轮播
*/
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleWithFixedDelay(
new ViewPageTask(),
2,
2,
TimeUnit.SECONDS);
}
/**
* 图片轮播任务
* @author liuyazhuang
*
*/
private class ViewPageTask implements Runnable{
@Override
public void run() {
currentItem = (currentItem + 1) % imageIds.length;
mHandler.sendEmptyMessage(0);
}
}
/**
* 接收子线程传递过来的数据
*/
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
mViewPaper.setCurrentItem(currentItem);
};
};
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(scheduledExecutorService != null){
scheduledExecutorService.shutdown();
scheduledExecutorService = null;
}
}
class MyBaseAdapter extends BaseAdapter{
@Override
public int getCount(){return name.length;}
@Override
public Object getItem(int i){return name[i];}
@Override
public long getItemId(int i){return i;}
@Override
public View getView(int i ,View convertView, ViewGroup parent) {
View view=View.inflate(musicFragment.this.getActivity(),R.layout.item_layout,null);
TextView tv_name=view.findViewById(R.id.item_name);
ImageView iv=view.findViewById(R.id.iv);
ImageView bf=view.findViewById(R.id.bf);
tv_name.setText(name[i]);
iv.setImageResource(icons[i]);
return view;
}
}
}
对应的xml界面文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="213dp"></androidx.viewpager.widget.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dip"
android:layout_gravity="bottom"
android:background="@drawable/pure"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片标题"
android:textColor="#0C0C0C" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:orientation="horizontal" >
<View
android:id="@+id/dot_0"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="#66BD0F32"/>
<View
android:id="@+id/dot_1"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="#66BD0F32"/>
<View
android:id="@+id/dot_2"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="#66BD0F32"/>
<View
android:id="@+id/dot_3"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="#66BD0F32"/>
<View
android:id="@+id/dot_4"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="#66BD0F32"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="83dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/m1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ylu" />
<ImageView
android:layout_weight="1"
android:id="@+id/m2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/phbang" />
<ImageView
android:layout_weight="1"
android:id="@+id/m3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/flei" />
<ImageView
android:layout_weight="1"
android:id="@+id/m4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/cnxhuan" />
</LinearLayout>
<ListView
android:background="@drawable/yybj1"
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
效果截图如下:
?
?
|