TV GridView中的item的焦点放大

1. 原理分析

一般我们在做获得所有布局的时候,可能需要让焦点放大然后加上焦点框的显示效果,这里除了可以使用GridView来进行实现,Recyclerview也可以做到,我这里在Tools中放了相关的缩放工具类,这里就不做讲解了。
在gridview中的item,想要实现放大的效果,我们可以使用属性动画来做。
这里我们把放大可能造成的一些错误的显示放到后面来讲。

2. 动画

这里我们写一个缩放的动画。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.15"
        android:fromYScale="1.0"
        android:toYScale="1.15"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="300"
        />
</set>

3. GridView的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent"
    tools:context="com.luo.gridview01.MainActivity">

    <GridView
        android:id="@+id/gv_all"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="@dimen/y253"
        android:listSelector="@android:color/transparent"
        android:numColumns="6"
        android:paddingBottom="@dimen/y20"
        android:paddingEnd="@dimen/y20"
        android:paddingLeft="@dimen/y50"
        android:paddingRight="@dimen/y46"
        android:paddingTop="@dimen/x19">
    </GridView>
</RelativeLayout>

4. item的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/item_app_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/y10"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:id="@+id/item_app_icon"
                android:layout_width="@dimen/x85"
                android:layout_height="@dimen/x85" />
        </LinearLayout>

        <!-- 下面textview处理了当文字过长的时候,用跑马灯的形式滚动文字 -->
        <TextView
            android:id="@+id/item_app_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/y8"
            android:layout_marginTop="@dimen/y8"
            android:ellipsize="marquee"
            android:focusable="true"
            android:gravity="center"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:textColor="#ffffff"
            android:textSize="@dimen/x22" />
    </LinearLayout>

</RelativeLayout>

5. Item的适配器

package com.luo.gridview01.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luo.gridview01.R;
import com.luo.gridview01.bean.AppBean;

import java.util.List;

/**
 * Created by luo on 2020/4/16.
 * app的适配器
 */

public class AppAdapter extends BaseAdapter {

    private static final String TAG = "AppAdapter";

    private Context mContext;
    private List<AppBean> appList;
    private LayoutInflater mLayoutInflater;
    private int selectid = -1;

    public AppAdapter(Context mContext, List<AppBean> appList) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.appList = appList;
    }

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

    @Override
    public Object getItem(int position) {
        return appList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //用于setOnItemSelectedListener中调用,赋值当前选择的item的position
    public void notifyDataSetChanged(int id) {
        selectid = id;
        super.notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mLayoutInflater.inflate(R.layout.item_layout, null);
        TextView appName = (TextView) convertView.findViewById(R.id.item_app_name);
        ImageView appIcon = (ImageView) convertView.findViewById(R.id.item_app_icon);
        LinearLayout appLayout = (LinearLayout) convertView.findViewById(R.id.item_app_layout);
        AppBean appBean = appList.get(position);
        appName.setText(appBean.getName());
        appIcon.setImageResource(appBean.getIcon());
        appLayout.setBackground(null);
        if (selectid == position) {
            // 开启动画
            Log.d(TAG, "selectid = " + selectid);
            Animation testAnim = AnimationUtils.loadAnimation(mContext, R.anim.app_scale);
            appLayout.setBackgroundResource(R.drawable.app_item_bg);
            convertView.startAnimation(testAnim);
        }
        return convertView;
    }
}

下面是焦点框的绘制

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke
        android:width="@dimen/x4"
        android:color="#ffffff" />
    <corners android:radius="@dimen/x12" />
</shape>

6. MainActivity的处理

package com.luo.gridview01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.luo.gridview01.adapter.AppAdapter;
import com.luo.gridview01.bean.AppBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo on 2020/4/16.
 *
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private GridView mGvApp;
    private List<AppBean> appList;
    private AppAdapter appAdapter;

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

    private void initData() {
        appList = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            AppBean appBean = new AppBean();
            appBean.setName("name : " + i);
            appBean.setIcon(R.drawable.smile);
            appList.add(appBean);
        }
    }

    private void initView() {
        mGvApp = (GridView) findViewById(R.id.gv_all);
        appAdapter = new AppAdapter(this, appList);
        mGvApp.setAdapter(appAdapter);
    }

    private void initListener() {
        mGvApp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "item = " + position, Toast.LENGTH_SHORT).show();
            }
        });
        mGvApp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //对适配器进行刷新
                AppAdapter adapter = (AppAdapter) mGvApp.getAdapter();
                adapter.notifyDataSetChanged(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

这里的AppBean 就不在这里列出来。

7.问题

当按照上面的代码进行编译的时候会出现下面图片的情况

image.png

现在我们怎么办呢?
更改GridView的内边距
并且加上下面
android:clipToPadding="false"
下面是更改后的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent"
    tools:context="com.luo.gridview01.MainActivity">

    <GridView
        android:id="@+id/gv_all"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="@dimen/y253"
        android:listSelector="@android:color/transparent"
        android:numColumns="6"
        android:clipToPadding="false"
        android:verticalSpacing="@dimen/x20"
        android:paddingBottom="@dimen/y40"
        android:paddingLeft="@dimen/y80"
        android:paddingRight="@dimen/y80"
        android:paddingTop="@dimen/y50">
    </GridView>

效果图


image.png

这样就达到了放大和焦点框的放大效果。

8. 总结

  1. 一个是属性动画的使用
  2. gridview的内边距的调整

下面是源码路径
GitHub:https://github.com/githubsmallluo/Gridview01

可以给作者评论,关注加喜欢吗?

如果还存在不懂的地方可以联系一下作者,我会帮忙解答!

QAQ.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,440评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,814评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,427评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,710评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,625评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,014评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,511评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,162评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,311评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,262评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,278评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,989评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,583评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,664评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,904评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,274评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,856评论 2 339

推荐阅读更多精彩内容

  • 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图...
    万忍阅读 1,496评论 0 6
  • 自己总结的Android开源项目及库。 github排名https://github.com/trending,g...
    passiontim阅读 2,529评论 1 26
  • 1、重要基本粒子 质子或中子 1.6x10^-15m~1.7x10^-15m 电子 5.64x10^-15米 1M...
    Minecraft尘云星_阅读 1,800评论 2 4
  • 文/妖怪来也 这一天,蔡锷带着小凤仙漫无目的到处转了半天,回到旅店后,对她悄悄说:“凤仙,我们这几天又被人跟踪了。...
    妖怪来也阅读 2,727评论 79 63
  • 文|悟恩居士图|源自网络 短篇小说因为篇幅小,所以一般语言比较精炼,不能像长篇那样长篇累牍,所以对于能够突出人物的...
    悟恩说事阅读 1,430评论 1 7