Chasing 168, Glamorgan could only manage 145-9 despite Graham Wagg's 50 off 28 balls.
Lewis McManus clubbed four sixes in his 59 off 35 and George Bailey hit 37 not out as Hampshire accelerated to 167-5.
James Vince hit a quickfire hit 31 early on before Michael Hogan (2-25) and Andrew Salter pegged them back.
McManus's late blitz meant the target looked a challenging one for Glamorgan, despite Salter's off-spin (1-10 in 3 overs) putting the brakes on in mid-innings.
Hampshire paceman Reece Topley claimed two wickets in the first over, and despite a stand of 41 from Aneurin Donald and Jacques Rudolph, leg-spinner Afridi's three wickets in two overs set them back again.
Wagg's explosive innings put Glamorgan back in the hunt as he added 62 for the sixth wicket with Chris Cooke, but further wickets from Topley and Afridi finished off the home challenge.
Glamorgan all-rounder Graham Wagg told BBC Wales Sport:
"Disappointing, it was lovely to see nearly seven thousand here, we sort of let them down but it was the first game so not too disheartened, we showed some good stuff in between some bad stuff. .
"It doesn't help when someone (Afridi) is on a hat-trick in T20- he had plenty to say when I was batting, there was a bit of banter but he bowled fantastically well though the wicket did help him, what a spell!
"I felt good but it was a shame Chris Cooke got out to Shahid Afridi's last over, if we'd got into the last five overs together it was on the cards to win, so I'm pretty gutted."
Hampshire captain James Vince told BBC Radio Solent:
"Great to get a win especially away from home to start the competition, in the last couple of years we haven't started as well as we would have liked, and since we didn't have any time to prepare it was very good to get that win.
"We started well, had a pretty slow patch and lost some wickets, then Bails (George Bailey) and Lew (McManus) timed the innings pretty well there, calm under pressure and the striking from Lew was great to see.
"Shahid got the rewards of the wickets but on a ground where it can be difficult to bowl spin, both he and Mason (Crane) did really well, and hopefully getting those wickets will get Reece back to where he can be."
Hampshire's Pakistani star Shahid Afridi destroyed Glamorgan with 4-20 as the visitors won by 22 runs in their T20 Blast opener in Cardiff.